游标的详细用法


游标的详细用法

文章插图
【游标的详细用法】declare @id int\猜首睁x0d\x0adeclare @name varchar(50)\x0d\x0adeclare cursor1 cursor for --定义游标cursor1\x0d\x0aselect * from table1 --使用游标的对象(跟据需要填入select文)\x0d\x0aopen cursor1 --打开游标\x0d\x0afetch next from cursor1 into @id,@name --将游标向下穗岁移1行,获取的数芹滚据放入之前定义的变量@id,@name中\x0d\x0awhile @@fetch_status=0 --判断是否成功获取数据\x0d\x0abegin\x0d\x0aupdate table1 set name=name+'1'\x0d\x0awhere id=@id --进行相应处理(跟据需要填入SQL文)\x0d\x0afetch next from cursor1 into @id,@name --将游标向下移1行\x0d\x0aend\x0d\x0aclose cursor1 --关闭游标\x0d\x0adeallocate cursor1

    推荐阅读