游标Cursor 使用小例 (SQLServer)

1 表table:

表名:TblTest
column  type length
-------------------------------
iId  int 4
testvalue int 4
testDesc char 10

2 游标示例

declare @MyTestCursor Cursor
 
set @MyTestCursor= Cursor for select iId,testvalue from TblTest

open @MyTestCursor
declare @iTestId int
declare @iTestValue int

fetch @MyTestCursor into @iTestId,@iTestValue

while @@fetch_status=0
 begin
  update TblTest set testvalue=floor(100*rand()) where iId=@iTestId
  fetch next from @MyTestCursor into @iTestId,@iTestValue
 end

select @iTestId as iOutId,@iTestValue as iOutValue

close @MyTestCursor
deallocate @MyTestCursor


3 讲解说明

数据库测试中,常常需要对数据库中的表进行填充或者批量更改数据的操作,可以通过游标来实现对每一个查询记录的操作,通过rand()函数

的使用获得随机数,将随机数插入到表中,即可更新或填充数据表。

    这里涉及到游标的使用,使用游标大体需要经过以下几个步骤:
  1.定义游标:declare cursor
  2.打开游标:open cursor
  3.取得游标中单个的记录,并将记录中的字段赋值给变量。fetch cursor
   (每取一个值,游标会自动前移)
  4.循环读取游标,并对每一个记录进行处理。fetch与fetch next 是等价的。
  5.关闭 并 释放游标,close cursor, deallocate cursor。


下面给出一个批量更改数据库中记录的例子,这个例子把测试表中所有测试值的值用0到100之间的数值更新,原测试表中所有测试值的值都为0

,更新之后所有的测试值都是0到100之间的随机数:

 -- 定义游标MyTestCursor:
 declare @MyTestCursor Cursor
 
 set @MyTestCursor= Cursor for select iId,testvalue from TblTest

 /*从表中选取两个字段*/
 -- 打开游标MyTestCursor:
 open @MyTestCursor
 declare @iTestId int
 declare @iTestValue int

 --fetch取出游标所指的记录,并将记录结果存入到变量中:
 fetch from @MyTestCursor into @iTestId,@iTestValue
 /***************** begin of loop *******************************/
 while @@fetch_status=0
  begin
   update TblTest set testvalue=floor(100*rand()) where iId=@iTestId
   fetch next from @MyTestCursor into @iTestId,@iTestValue
  end
 /***************** end of loop *******************************/

 select @iTestId as iOutId,@iTestValue as iOutValue
 
 /***********关闭游标释放游标:***************/
 close @MyTestCursor
 deallocate @MyTestCursor

    ☆☆☆再重复一下,使用游标批量更改或填充数据库,大体经过declare,open,fetch,loop fetch,close and deallocate 五个步骤。
  备注1:
   while循环体以BEGIN开始,以END结束,当条件为真时循环继续,为假则结束
  备注2:
   @@FETCH_STATUS是sql server中的一个变量,下面是SQL server Books online上的解释:
  Returns the status of the last cursor FETCH statement issued against any cursor currently opened by the connection.
  
  Return value
  Description
  0
  FETCH statement was successful.
  -1
  FETCH statement failed or the row was beyond the result set.
  -2
  Row fetched is missing.
  Examples
  This example uses @@FETCH_STATUS to control cursor activities in a WHILE loop.
  DECLARE Employee_Cursor CURSOR FOR
  SELECT LastName, FirstName FROM Northwind.dbo.Employees
  OPEN Employee_Cursor
  FETCH NEXT FROM Employee_Cursor
  WHILE @@FETCH_STATUS = 0
  BEGIN
   FETCH NEXT FROM Employee_Cursor
  END
  CLOSE Employee_Cursor
  DEALLOCATE Employee_Cursor

附:

.@@FETCH_STATUS属于任何游标的
----------------------------------------------------------------------
该文章转载自网络大本营:http://www.xrss.cn/Dev/DataBase/200791716603.Html

.@@FETCH_STATUS属于任何游标的,只要任何一个游标被提取了,这个提取成功与否的状态就会保存到@@FETCH_STATUS中.
嵌套游标的原理类似这样:
declare 外层游标
open 外层游标
fetch next ...提取外层游标行
while @@FETCH_STATUS = 0
begin
    declare 内层游标
    open 内层游标
    ftech next ...提取内层游标行
    while @@FETCH_STATUS = 0
    begin
          .....处理内层游标
          ftech next ....内层游标向下移动一行
    end
    close 内层游标
    deallocate 内层游标
    fetch next ....内层游标处理结束后,外层游标才继续向下移动一行
end
close 外层游标
deallocate 外层游标
也就是说,外层游标每移动一行,就要重复进行内层游标定义,打开,循环,关闭,释放等操作,然后才能再向下移动行.

原文地址:https://www.cnblogs.com/wbcms/p/2084388.html