首页 > 代码库 > SQL SERVER 游标的使用

SQL SERVER 游标的使用

--基于查询声明游标declare cur CURSOR FORselect id from OrderProducts where OrderCode =@OrderCode    --select @OrderProductsId = id from OrderProducts where OrderCode =@OrderCode        --打开游标    OPEN cur    --读取下一个游标    FETCH NEXT FROM cur INTO @OrderProductsId        --遍历游标记录,直至到达游标的末尾    WHILE (@@FETCH_STATUS = 0)    BEGINIF @OrderProductsId <> ‘‘ or @OrderProductsId is not nullbegin   IF NOT EXISTS(SELECT 1 FROM OrderProducts WHERE IsInvented=0 AND id=@OrderProductsId) /*判断订单号是否是虚拟商品*/BEGINSET @error=-1080;GOTO FailedLabel;ENDend   FETCH NEXT FROM cur INTO @OrderProductsIdEND--关闭游标CLOSE cur--释放游标DEALLOCATE cur