近期又看到头疼的cursor的问题。把游标重新整理下吧
1 declare @id varchar(50);
2 declare @Title varchar(50);
3 declare @Director varchar(50);
4 declare @DateReleased varchar(50);
5
6 declare mycursor cursor for --定义一个游标
7 select * from movies --游标的可循环对象
8 open mycursor --打开游标
9 fetch next from mycursor into @id,@Title,@Director,@DateReleased --移动游标到下一条数据(第一条),获取数据@id,@Title,@Director,@DateReleased(数目必须相同)
10 while(@@FETCH_STATUS=0) --判断是否成功获取数据
11 /*
12 @@FETCH_STATUS =0 FETCH 语句成功
13 @@FETCH_STATUS =-1 FETCH 语句失败或此行不在结果集中
14 @@FETCH_STATUS =-2 被提取的行不存在
15 */
16
17 begin
18
19 print (@Title) --对游标的操作
20 fetch next from mycursor into @id,@Title,@Director,@DateReleased --将游标向下移1行
21 end
22 close mycursor --关闭游标
23 deallocate mycursor --删除游标