Sql游标的使用

Create Proc sp_Cursor
(
 nvarchar(50) @GPGENNo
)
as
Declare @ErrorCode nvarchar(MAX)
begin Tran

---- 例子
          declare @TaskDate datetime

          declare @Id int
          declare @cur cursor
          set @cur=cursor for select TaskDate,Id   from #Temp1 Order By TaskDate
          open @cur
          fetch next from @cur into @TaskDate,@Id
          while @@fetch_status=0
          begin


                Update TableName1 Set TaskDate=@TaskDate  Where Id=@Id    

              
                fetch next from @cur into @TaskDate  , @Id
          end
          close @cur
          deallocate @cur


  if @@error<>0 goto Err
 
  Drop Table #Temp1


Commit Tran
return
Err:
    select @ErrorCode as ErrorCode
    rollback tran
    return

原文地址:https://www.cnblogs.com/Gxiaopan/p/4183916.html