下面为您介绍的例子共同使用了SQL Server游标和SQL Server存储过程,如果您SQL Server游标和存储过程都比较感兴趣的话,不妨一看。
- If Object_ID('dbo.GetMasterGoods') Is Not Null
- Drop Proc dbo.GetMasterGoods
- Go
- Create Proc GetMasterGoods
- @MyCursor Cursor Varying Output
- With Encryption
- As
- Set @MyCursor = Cursor
- For
- Select GoodsCode,GoodsName From Master_Goods
- Open @MyCursor
- Go
- --下边建立另外一个存储过程,用于遍历游标输出结果
- Create Proc GetAllGoodsIDAndName
- As
- Declare @GoodsCode varchar(18)
- Declare @GoodsName nvarchar(20)
- Declare @MasterGoodsCursor Cursor
- Exec GetMasterGoods @MasterGoodsCursor out
- Fetch Next From @MasterGoodsCursor
- InTo @GoodsCode,@GoodsName
- While(@@Fetch_Status = 0)
- Begin
- Begin
- Print @GoodsCode + ':' + @GoodsName
- End
- Fetch Next From @MasterGoodsCursor
- InTo @GoodsCode,@GoodsName
- End
- Close @MasterGoodsCursor
- Deallocate @MasterGoodsCursor
- Go
以上就是SQL Server游标和存储过程共同使用的方法。
【编辑推荐】