下面这个就是标准的游标访问方式DECLARE authors_cursor CURSOR FOR
SELECT au_lname FROM authors --指定游标访问的表
OPEN authors_cursor --打开游标
FETCH NEXT FROM authors_cursor --取下一个游标指向的值
SELECT @@CURSOR_ROWS
CLOSE authors_cursor --关闭游标
DEALLOCATE authors_cursor --释放游标
SELECT au_lname FROM authors --指定游标访问的表
OPEN authors_cursor --打开游标
FETCH NEXT FROM authors_cursor --取下一个游标指向的值
SELECT @@CURSOR_ROWS
CLOSE authors_cursor --关闭游标
DEALLOCATE authors_cursor --释放游标
第一行是返回值是第一个SELECT @@CURSOR_ROWS返回的,你没有定义之前,游标的状态。--还没有被打开。
第二行是定义了游标以后,进入第一个循环,FETCH NEXT FROM authors_cursor载入第一个值。--返回white
第三行是 由第二个SELECT @@CURSOR_ROWS返回的游标状态,以被定义,使用中。循环还没结束。
参考 http://technet.microsoft.com/zh-cn/library/ms188667(v=sql.105).aspx异步填充不方便做DEMO,其他返回值请参考如下例子.create table cxy
(sno int not null,
sname varchar(10)
constraint pk_cxy primary key(sno)
)insert into cxy
select 1,'aaa' union all
select 2,'bbb' union all
select 3,'ccc' union all
select 4,'ddd' union all
select 5,'eee'
-- -1 游标为动态游标。
declare ap cursor dynamic
for select sno,sname from cxy where sno<=3open apselect @@CURSOR_ROWS 'cursor_rows'
/*
cursor_rows
-----------
-1(1 row(s) affected)
*/close ap
deallocate ap
-- 0 没有已打开的游标,对于上一个打开的游标没有符合条件的行,或上一个打开的游标已被关闭或被释放。
declare ap cursor static
for select sno,sname from cxy where 1=2open apselect @@CURSOR_ROWS 'cursor_rows'
/*
cursor_rows
-----------
0(1 row(s) affected)
*/close ap
deallocate ap
--n 游标已完全填充。返回值 (n) 是游标中的总行数。
declare ap cursor static
for select sno,sname from cxy where sno<=3open apselect @@CURSOR_ROWS 'cursor_rows'
/*
cursor_rows
-----------
3(1 row(s) affected)
*/close ap
deallocate ap