游标实例

    游标是一个数据库中的另类,但是有时候不得不用,如果要一行一行处理数据的时候,在服务器端处理,我们不得不用它。
  不得不注意的是:它复杂和低效,尤其在数量量大时候及其明显
   游标中,只读、单向的游标速度最快,而且也不容易造成死锁,尽可能用它

      标准游标:

            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      --释放游标

posted @ 2009-08-26 11:16  艾伦  阅读(696)  评论(0编辑  收藏  举报