SQL 游标

declare @tmpTbl Table(uid int)
declare @userid int
begin
begin TRAN LUOFUXIAN
INSERT INTO @tmpTbl(uid) SELECT userid FROM tbl_news
declare tmpCursor CURSOR
FOR SELECT uid FROM @tmpTbl
OPEN tmpCursor
FETCH NEXT FROM tmpCursor into @userid
while @@FETCH_STATUS=0
BEGIN
UPDATE tbl_users set username='_'+ cast(@userid as nvarchar(255))+'_'
where tbl_users.id=@userid
FETCH NEXT FROM tmpCursor into @userid
END
close tmpcursor
deallocate tmpcursor
if(@@ERROR>0)
BEGIN
ROLLBACK TRAN LUOFUXIAN
END
commit TRAN LUOFUXIAN
end
posted @ 2011-12-07 14:21  流浪者的飘  阅读(180)  评论(0编辑  收藏  举报