经常写存储过程,但今天在游标使用过程中还是疏忽了一些事情,执行过程中一直执行不下去,后来直接sqlserver挂了,教训啊! 代码虽简单,望铭记: Create PROCEDURE [dbo].[temphxb] AS BEGIN declare @uid int declare mycursortemp Cursor for select uid from temptable1 where Indate>'2015-05-21' and type='1' group by uid having COUNT(*)>1 open mycursortemp fetch next from mycursortemp into @uid while @@FETCH_STATUS=0 begin delete from temptable1 where id in (select top 1 id from temptable1 where uid=@uid and Indate>'2015-05-21' and fdtype='1') update temptable2 set num=num+1 where uid=@uid fetch next from mycursortemp into @uid end close mycursortemp DEALLOCATE mycursortemp end
游标使用过程中,一般分为以下五个步骤: 2、打开游标 3、使用游标 4、关闭游标 5、删除游标
今天本人就是在第三步游标遍历数据中忘记去执行 fetch next from mycursortemp into @uid,导致死循环发生,造成不小的损失。 数据库操作需要谨慎谨慎,切记。 (责任编辑:IT) |