数据库操作9.6

标签: 游标  
2011-05-18 22:21 阅读(?)评论(0)

    游标(读取游标中数据)

  当打开一个游标之后,就可以读取游标中的数据了。可以使用Fetch命令读取游标中的某一行数据。
语法:
Fetch
[[next|prior|first|last
        |absolute{n|@var}
        |relative{n|@nvar}
]
From ]{{[global]cursor_name}|@cursor_variable_name [into @variable_name[,….n]]
Eg:
declare  emp_cursor cursor
for select 姓名,性别 from tb_emp06
open emp_cursor
declare @name varchar(8),@sex char(2)
fetch next from emp_cursor into @name,@sex
print '姓名:'+@name+'性别:'+@sex
fetch next from emp_cursor into @name,@sex
print '姓名:'+@name+'性别:'+@sex
Close emp_cursor
deallocate emp_cursor

读取游标中的数据

@@fetch_status
返回值0: fetch语句成功
返回值-1:fetch语句失败或此行不在结果集中
返回值-2:被提取的行不存在。
Eg:
Declare readcursor cursor for
Select 编号,姓名,性别,所属部门
From tb_employee
Open readcursor
Fetch next from readcursor
While @@fetch_status=0
   begin
        fetch next from readcursor
   end

 关闭/释放游标

 语法:
Close {{{Global] cursor_name}]|cursor_variable_name}---关闭游标
Deallocate {{{Global] cursor_name}]|cursor_variable_name}--释放游标

  

 
表  情:
加载中...
 

请各位遵纪守法并注意语言文明