How to Use Cursor

来源:互联网 发布:linux vi显示行数 编辑:程序博客网 时间:2024/05/08 07:17

Below link is a good source for learning cursors in sql 

http://www.cnblogs.com/moss_tan_jun/archive/2011/11/26/2263988.html 

 

And I already made a little script for  example. 

The function of this script is, to check  id & dcid for all students, 

Then they 2 are not equal then change make this id to dcid's value. 

And delete this cursor to free space in RAM. 

Please make a cursor by yourself! 

 

DECLARE 

test_cursor CURSOR  

FOR 

select id,DCID FROM STUDENTS where dcid!=id 

OPEN test_cursor 

DECLARE @ID NVARCHAR(10) 

DECLARE @DCID NVARCHAR(10) 

FETCH NEXT FROM test_cursor INTO @ID,@DCID 

while @@fetch_status=0 

begin 

print @id 

print @dcid 

update students set id=@dcid 

where dcid=@dcid 

fetch next from test_cursor into @id,@dcid 

end 

close test_cursor 

deallocate test_cursor 

 

Feel free to let me know if you have any question about this. 

 

FYI, CSDN forum is a good source for learning everything about PC/MAC. 

Here is a link, you can login with WeChat QR code. 

http://www.csdn.net/

0 0
原创粉丝点击