游标处理

declare m_cursor cursor scroll for
select a.CreateTime,s.CommentID from Comment s inner join Article a on s.ObjectID=a.ArticleID where ArticleID=122
for update
-- 打开游标
open m_cursor
declare @dataTimes Datetime, @CommentId int
--填充数据
fetch next from m_cursor into @dataTimes,@CommentId
--假如检索到了数据,才处理
while @@FETCH_STATUS=0
begin

update Comment set CreateTime=dateadd(MINUTE,CEILING(rand()*1200),cast(@dataTimes as datetime)) where CommentId=@CommentId
--填充下一条数据
fetch next from m_cursor into @dataTimes,@CommentId
end
-- 关闭游标
close m_cursor
--释放游标
deallocate m_cursor

原文地址:https://www.cnblogs.com/xujie520/p/6531676.html