文章来源:SQL Server游标 - 张龙豪 - 博客园 (cnblogs.com)
--5.利用游标更新删除数据 ---游标更新删除当前数据 ---1.声明游标 declare orderNum_03_cursor cursor scroll for select OrderId ,userId from bigorder where orderNum='ZEORD003402' --2.打开游标 open orderNum_03_cursor --3.声明游标提取数据所要存放的变量 declare @OrderId int ,@userId varchar(15) --4.定位游标到哪一行 fetch First from orderNum_03_cursor into @OrderId,@userId --into的变量数量必须与游标查询结果集的列数相同 while @@fetch_status=0 --提取成功,进行下一条数据的提取操作 begin if @OrderId=122182 begin Update bigorder Set UserId='123' Where Current of orderNum_03_cursor --修改当前行 end if @OrderId=154074 begin Delete bigorder Where Current of orderNum_03_cursor --删除当前行 end fetch next from orderNum_03_cursor into @OrderId ,@userId --移动游标 end --示例2 declare @id char(50) declare @names char(50) declare @age int set @age=27 declare @ages int declare cur_name cursor for --声明游标 select id,names,age from table_name open cur_name --打开游标 fetch next from cur_name into @id,@names,@ages --获取游标的下一行 while @@FETCH_STATUS=0 begin if @age=@ages begin delete from table_name where ages=@ages --执行删除操作 end fetch next from cur_name into @id,@names,@ages --获取游标的下一行 end close cur_name --关闭游标 deallocate cur_name --释放游标
标签:orderNum,name,--,游标,Server,cursor,SQL,declare From: https://www.cnblogs.com/xjxue/p/17643918.html