declare my_cursor cursor scroll dynamic

 for
 select * from t_msg open my_cursor
 declare @name sysname
 fetch next from my_cursor into @name
 while(@@fetch_status=0)
 begin
 print 'UserName: ' + @name
 --fetch next from my_cursor
 fetch next from my_cursor into @name
 end --fetch first from my_cursor into @name
 print @name


 close my_cursor
 deallocate my_cursor

使用游标(cursor)的一个主要的原因就是把集合操作转换成单个记录处理方式。用SQL语言从数据库中检索数据后,结果放在内存的一块区域中,且结果往往是一个含有多个记录的集合。游标机制允许用户在SQL server内逐行地访问这些记录,按照用户自己的意愿来显示和处理这些记录。
2. 如何使用游标:
     一般地,使用游标都遵循下列的常规步骤:
      (1) 声明游标。把游标与T-SQL语句的结果集联系起来。
      (2) 打开游标。
      (3) 使用游标操作数据。
      (4) 关闭游标。
2.1. 声明游标
DECLARE CURSOR语句SQL-92标准语法格式:

DECLARE 游标名 [ INSENSITIVE ] [ SCROLL ] CURSOR
 FOR sql-statement
 Eg:
 Declare MycrsrVar Cursor
 FOR Select * FROM tbMyData


2.2 打开游标
OPEN MycrsrVar
当游标被打开时,行指针将指向该游标集第1行之前,如果要读取游标集中的第1行数据,必须移动行指针使其指向第1行。就本例而言,可以使用下列操作读取第1行数据:

FETCH FIRST from E1cursor
      或 FETCH NEXT from E1cursor


2.3      使用游标操作数据   
下面的示例用@@FETCH_STATUS控制在一个WHILE循环中的游标活动

DECLARE E1cursor cursor     
 FOR SELECT * FROM c_example
 OPEN E1cursor               
 FETCH NEXT from E1cursor    
 WHILE @@FETCH_STATUS = 0    
 BEGIN
 FETCH NEXT from E1cursor  
 END             
 DEALLOCATE E1cursor             关闭游标
      使用CLOSE语句关闭游标
 CLOSE { { [ GLOBAL ] 游标名 } | 游标变量名 }
 使用DEALLOCATE语句删除游标,其语法格式如下:
 DEALLOCATE { { [ GLOBAL ] 游标名 } | @游标变量名
 3. FETCH操作的简明语法如下:
 FETCH
            [ NEXT | PRIOR | FIRST | LAST]
 FROM
 { 游标名 | @游标变量名 } [ INTO @变量名 [,…] ]

参数说明:

 

INTO @变量名[,…] 把提取操作的列数据放到局部变量中。列表中的各个变量从左到右与游标结果集中的相应列相关联。各变量的数据类型必须与相应的结果列的数据类型匹配或是结果列数据类型所支持的隐性转换。变量的数目必须与游标选择列表中的列的数目一致。

--------------------------------------------------------------------------------------------------------------------------------

每执行一个FETCH操作之后,通常都要查看一下全局变量@@FETCH_STATUS中的状态值,以此判断FETCH操作是否成功。该变量有三种状态值:

? 0 表示成功执行FETCH语句。

? -1 表示FETCH语句失败,例如移动行指针使其超出了结果集。

? -2 表示被提取的行不存在。

由于@@FETCH_STATU是全局变量,在一个连接上的所有游标都可能影响该变量的值。因此,在执行一条FETCH语句后,必须在对另一游标执行另一FETCH 语句之前测试该变量的值才能作出正确的判断。

更新数据;

declare my_youbiao cursor
 for select * from t_msg
 for update open my_youbiao
 fetch next from my_youbiao
 while @@fetch_status=0
 begin
 --update t_msg set msg='1234567890' where current of my_youbiao
 update my_youbiao set msg='123' where current of my_youbiao
 fetch next from my_youbiao
 print 'asdfasd11'
 end
 close my_youbiao
 deallocate my_youbiao
 print 'asdfasd'

测试通过:


-- 
  select * from    
  
 
  use  
  test
 
  declare  
  my_cursor  
  cursor  
  scroll dynamic  
  -- 
  scroll表示可以向前或向后移动    
  
 
  for 
  
 
  select  
  F3  
  from  
  temp  
  -- 
  定义my_cursor 游标 
  
 
  
 
  open  
  my_cursor  
  -- 
  打开游标 
  
 
  declare  
  @name  
  nvarchar 
  ( 
  128 
  )  
  -- 
  定义一个变量 
  
 
  fetch  
  next  
  from  
  my_cursor  
  into  
  @name  
  -- 
  游标停在第一条记录前面,第一次执行,测试有没有记录存在 
  
 
  while 
  ( 
  @@fetch_status 
  = 
  0 
  )  
  -- 
  取数据,直到-2即没有记录 
  
 
  begin 
  
 
  print  
  ' 
  姓名:  
  '  
  +  
  @name  
  
 
  -- 
  fetch next from my_cursor 
  
 
  fetch  
  next  
  from  
  my_cursor  
  into  
  @name 
  
 
  end 
  

 
  -- 
  fetch first from my_cursor into @name 
  
 
  print  
  @name 
  
  
  -- 
  update temp set F9='zzg' where current of my_cursor  
  
 
  
 
  close  
  my_cursor
 
  deallocate  
  my_cursor