sql server游标实例:
复制代码 代码示例:
Declare MyCusror Cursor Scroll
For Select * From Master_Goods Order By GoodsID
Open MyCursor
Fetch next From MyCursor
Into @GoodsCode,@GoodsName
While(@@Fetch_Status = 0)
Begin
Begin
Select @GoodsCode = Convert(Char(20),@GoodsCode)
Select @GoodsName = Convert(Char(20),@GoodsName)
PRINT @GoodsCode + : + @GoodsName
End
Fetch next From MyCursor
Into @GoodsCode,@GoodsName
End
Close MyCursor
Deallocate MyCursor
修改当前游标行数据:
UpDate Master_Goods Set GoodsName = php100 Where Current Of MyCursor
删除当前游标行数据:
Delete From Master_Goods Where Current Of MyCursor
Select @@CURSOR_ROWS 可以得到当前游标中存在的数据行数。
注意:此变量为一个连接上的全局变量,因此只对应最后一次打开的游标。
打开游标
全局游标:Open Global MyCursor
局部游标: Open MyCursor