利用游标在存储过程中实现循环
我只在sql server 2005上测试通过
ALTER proc proc_ChangTaskPriority
(
@ID int
)
as
declare @xh int
declare @id_temp int
--定义游标
declare XH_cursor cursor for select t.XH from TableName t where t.ID > @ID
--打开游标
open XH_cursor
--从游标取记录
fetch next from XH_cursor into @xh
--当有记录
while @@fetch_status=0
begin
select @id_temp = tt.ID from TableName tt where tt.XH = @xh
update TableName set xh = @xh-1 where id = @id_temp
--取下一条记录
fetch next from XH_cursor into @xh
end
--关闭游标
close XH_cursor
--删除游标引用
deallocate XH_cursor
浙公网安备 33010602011771号