游标实例
不得不注意的是:它复杂和低效,尤其在数量量大时候及其明显
游标中,只读、单向的游标速度最快,而且也不容易造成死锁,尽可能用它
标准游标:
Declare MyCursor Cursor
For Select * From Master_Goods
只读游标
Declare MyCusror Cursor
For Select * From Master_Goods
For Read Only
可更新游标
Declare MyCusror Cursor
For Select * From Master_Goods
For UpDate
游标实例:
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 --释放游标