首页 > 代码库 > Sql游标的使用

Sql游标的使用

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

---- 例子
  select * into #TempGPGENDetail from GPGENDetail where GPGenNo=@GPGENNo
 declare @cur cursor
 declare @GENNo nvarchar(50)
 declare @GoldWtGM numeric(18,2)
 declare @GENGoldWtGM numeric(18,2)
       
  set @cur=cursor for select GENNo,GoldWtGM from #TempGPGENDetail
  open @cur
  fetch next from @cur into @GENNo,@GoldWtGM
  while @@fetch_status=0
  begin
     
   select @GENGoldWtGM=SUM(GoldWtGM) from GPGENProduct where GENNo=@GENNo
   
   if @GoldWtGM>=@GENGoldWtGM
   begin
     Set @ErrorCode=N‘分货编码为:‘+@GENNo+‘的金重必须大于分货金重‘
     goto Err
   end
   
   fetch next from @cur into @GENNo,@GoldWtGM
  end
  close @cur
  deallocate @cur
  if @@error<>0 goto err
       
  Drop Table #TempGPGENDetail


end

Sql游标的使用