SqlServer游标简介

时间:2021-08-22 15:28:05

游标实例:

            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 = 'yangyang8848' Where Current Of MyCursor;

删除当前游标行数据的方法如下:

    Delete From Master_Goods Where Current Of MyCursor

Select @@CURSOR_ROWS 可以得到当前游标中存在的数据行数。

注意:此变量为一个连接上的全局变量,因此只对应最后一次打开的游标。