7.2
/*创建游标testCursorA,把testCursorA看成存放游标的容器,查询网站职员表的游标记录放入testCursorA,约束为制度游标*/
declare testCursorA cursor for
select *from AddressInfos
for read only/*当前为只读游标;去掉for read only,是标准游标;for update,是更新游标*/
/*打开游标*/
open testCursorA
/*使用游标,从testCursorA依次拿出放在testCursorA里面的游标*/
fetch nextfrom testCursorA
7.3游标定义格式
declare 游标名称cursor
[Local|Global]//游标的作用范围:局部|全局
[Forward_only|Scroll]//只进游标(从前往后走)|任意滚动
[Stattic|Keyset|Dynamic|Fast_froward]
[Reae Only|Scroll_Locks|Optimistic]
[Type_warning]
for
...Sql语句...
for [read only|update]
7.4
/*创建任意滚动游标*/
declare testCursorA cursor scroll for
select *from 网站职员表
/*调用查询下一个游标*/
fetch nextfrom testCursorA
/*调用查询第一个游标*/
fetch firstfrom testCursorA
/*调用查询最后一个游标*/
fetch lastfrom testCursorA
7.5
/*创建可滚动游标*/
declare testCursorB cursorscroll for
select *from AddressInfos
/*调用游标,从前往后获取游标记录*/
fetch priorfrom testCursorB
7.07
/*创建游标*/
declaretestCursorA cursor scrollfor
select *from dbo.第二网站职员表
/*打开游标*/
opentestCursorA
/*执行*/
fetch absolutefromtestCursorA
/*relative把游标向前或者向后移动一个位置;relative -1把游标向前移动一个位置;relative 1把游标向后移动一个位置*/
fetch relative-1 from testCursorA
7.08
declare@testvar1 int,@testVar2varchar(100),@testVar3int,@testVar4 varchar(100),@testVar5money
fetch relative-2 from testCursorAinto@testVar1,@testVar2,@testVar3,@testVar4,@testVar5
7.09
/*关闭游标*/
closetestCursorA
/*释放游标*/
deallocatetestCursorA