SQL Server游标和存储过程共同使用的例子


  本文标签:SQL Server游标

  下面为您介绍的例子共同使用了SQL Server游标和SQL Server存储过程,如果您SQL Server游标和存储过程都比较感兴趣的话,不妨一看  。

  1. If Object_ID(dbo.GetMasterGoods) Is Not Null  
  2.            Drop Proc dbo.GetMasterGoods  
  3.      Go  
  4.  
  5.      Create Proc GetMasterGoods  
  6.      @MyCursor Cursor Varying Output  
  7.      With Encryption  
  8.      As   
  9.             Set @MyCursor = Cursor  
  10.             For  
  11.                    Select GoodsCode,GoodsName From Master_Goods  
  12.      Open @MyCursor  
  13.      Go  
  14.  
  15.      --下边建立另外一个存储过程,用于遍历游标输出结果  
  16.  
  17.      Create Proc GetAllGoodsIDAndName  
  18.      As  
  19.  
  20.      Declare @GoodsCode varchar(18)  
  21.      Declare @GoodsName nvarchar(20)  
  22.      Declare @MasterGoodsCursor Cursor  
  23.      Exec GetMasterGoods @MasterGoodsCursor out  
  24.      Fetch Next From @MasterGoodsCursor  
  25.      InTo @GoodsCode,@GoodsName  
  26.      While(@@Fetch_Status = 0)  
  27.      Begin  
  28.             Begin  
  29.                    Print @GoodsCode + : + @GoodsName  
  30.             End  
  31.             Fetch Next From @MasterGoodsCursor  
  32.             InTo @GoodsCode,@GoodsName  
  33.      End  
  34.      Close @MasterGoodsCursor  
  35.      Deallocate @MasterGoodsCursor  
  36.      Go  

  以上就是SQL Server游标和存储过程共同使用的方法  。