SQL cursor usage and examples

declare my_cursor cursor scroll dynamic
/*scroll means that the cursor pointer can be moved at will (otherwise only forward), dynamic means that the cursor can be read and written (otherwise the cursor is read-only)*/
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
/* update profile set name='zzg' where current of my_cursor */
/* delete from profile where current of my_cursor */
close my_cursor
deallocate my_cursor

 

One of the main reasons for using cursors is to convert collection operations into single-record processing. After retrieving data from a database in SQL, the result is placed in an area of ​​memory, and the result is often a collection of multiple records. The cursor mechanism allows users to access these records row by row in the SQL server, and display and process these records according to the user's own wishes.
2. How to use the cursor:
     Generally, the following general steps are followed to use the cursor:
      (1) Declare the cursor. Associates the cursor with the result set of the T-SQL statement.
      (2) Open the cursor.
      (3) Use cursors to manipulate data.
      (4) Close the cursor.
2.1. Declare a cursor
DECLARE CURSOR statement SQL-92 standard syntax format:
DECLARE cursor name [ INSENSITIVE ] [ SCROLL ] CURSOR
FOR sql-statement
Eg:
Declare MycrsrVar Cursor
FOR Select * FROM tbMyData
2.2 Open cursor
OPEN MycrsrVar
When the cursor is opened, the row Before the pointer will point to the first row of the cursor set, if you want to read the first row of data in the cursor set, you must move the row pointer to point to the first row. For this example, row 1 data can be read using the following operations:
     FETCH FIRST from E1cursor
     or FETCH NEXT from E1cursor
2.3 Using the cursor to manipulate data   
The following example uses @@FETCH_STATUS to control the cursor activity in a WHILE loop
/* The operation of using the cursor to read data is as follows. */
DECLARE E1cursor cursor /* declare cursor, default is FORWARD_ONLY cursor */
FOR SELECT * FROM c_example
OPEN E1cursor /* open cursor */
FETCH NEXT from E1cursor /* read row 1 data */
WHILE @@FETCH_STATUS = 0 / * Use the WHILE loop to control cursor activity */
BEGIN
FETCH NEXT from E1cursor /* The rest of the row data will be read in the body of the loop */
END

CLOSE E1cursor /* close the cursor */
DEALLOCATE E1cursor /* delete the cursor */

2.4     关闭游标
     使用CLOSE语句关闭游标
CLOSE { { [ GLOBAL ] 游标名 } | 游标变量名 }
使用DEALLOCATE语句删除游标,其语法格式如下:
DEALLOCATE { { [ GLOBAL ] 游标名 } | @游标变量名
3. FETCH操作的简明语法如下:
FETCH
           [ NEXT | PRIOR | FIRST | LAST]
FROM
{ 游标名 | @游标变量名 } [ INTO @变量名 [,…] ]

参数说明:

NEXT   取下一行的数据,并把下一行作为当前行(递增)。由于打开游标后,行指针是指向该游标第1行之前,所以第一次执行FETCH NEXT操作将取得游标集中的第1行数据。NEXT为默认的游标提取选项。

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   master..sysprocesses
use test
declare my_cursor cursor scroll dynamic --scroll表示可以向前或向后移动   dynamic:表示可写也可读,
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 
/* delete from 个人资料 where current of my_cursor */
close my_cursor
deallocate my_cursor

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326053666&siteId=291194637