利用游标在存储过程中实现循环

我只在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
posted @ 2011-08-01 19:00  jeffreyQ  阅读(1498)  评论(0编辑  收藏  举报
跟小D每日学口语
跟小D每日学口语