SQL游标示例

DECLARE @@totalNum INT;
SET @@totalNum=0;
DECLARE @num INT;
DECLARE @CustomInfo NVARCHAR(MAX);
DECLARE My_Cursor CURSOR --定义游标
FOR (SELECT  b.CustomInfo FROM dbo.Orders a,dbo.ElongOrdersDetails b
WHERE a.OrderSn=b.OrderSn AND a.OrderType=18 AND a.Status in(2,5,6,10,11)) --查出需要的集合放到游标中
OPEN My_Cursor; --打开游标
FETCH NEXT FROM My_Cursor INTO @CustomInfo; --读取第一行数据
WHILE @@FETCH_STATUS = 0
    BEGIN
        SELECT @num=COUNT(1) FROM [dbo].[parseJSON](@CustomInfo) WHERE NAME='name'
        SET @@totalNum=@@totalNum+@num;
        FETCH NEXT FROM My_Cursor INTO  @CustomInfo ; --读取下一行数据
    END
CLOSE My_Cursor; --关闭游标
DEALLOCATE My_Cursor; --释放游标
PRINT @@totalNum
GO

参考:http://www.cnblogs.com/zengen/archive/2011/08/10/2133423.html

posted @ 2018-10-08 14:40  skybirdzw  阅读(1915)  评论(0编辑  收藏  举报