小鬼脸--ifunnyface
白云哮蓝天
1 /*游标的使用*/
2 --table1结构如下
3 --id int
4 --name varchar(50)
5
6 declare @id int
7 declare @name varchar(50)
8 declare cursor1 cursor for --定义游标cursor1
9 select * from table1 --使用游标的对象(跟据需要填入select文)
10 open cursor1 --打开游标
11
12 fetch next from cursor1 into @id,@name --将游标向下移1行,获取的数据放入之前定义的变量@id,@name中
13
14 while @@fetch_status=0 --判断是否成功获取数据
15 begin
16 update table1 set name=name+'1'
17 where id=@id --进行相应处理(跟据需要填入SQL文)
18
19 fetch next from cursor1 into @id,@name --将游标向下移1行
20 end
21
22 close cursor1 --关闭游标
23 deallocate cursor1 --完全删除游标
posted on 2010-12-24 23:32  ifunnyface  阅读(312)  评论(0编辑  收藏  举报