SQL 索引小例子,仅供参考一下格式。

DECLARE cur CURSOR
FOR
    SELECT  ID ,
            subNum
    FROM    dbo.StockLog_Tab
    WHERE   typeID = '3'
            AND ID <> '352'
    ORDER BY CreateDate DESC;
DECLARE @subNum FLOAT;
DECLARE @nextSubSum FLOAT;
DECLARE @subTotal INT;
DECLARE @result NVARCHAR(20);
DECLARE @ID INT;
SET @nextSubSum = -12400;
SET @subTotal = 34800;
OPEN cur;
--打开指针
FETCH NEXT FROM cur INTO @ID, @subNum;
WHILE ( @@FETCH_STATUS = 0 )
    BEGIN
    --print CAST(@subTotal AS NVARCHAR(20))+'+' + CAST(@subNum AS NVARCHAR(20)) +' : ' + CAST((@subTotal+@subNum) AS NVARCHAR(50));
    
        UPDATE  StockLog_Tab
        SET     subTotal = @subTotal + ( -1 * @nextSubSum )
        WHERE   ID = @ID;
 
        SET @subTotal = @subTotal + ( -1 * @nextSubSum );
        SET @nextSubSum = @subNum;
        FETCH NEXT FROM cur INTO @ID, @subNum;--指针下移
    END;
CLOSE cur;
--关闭指针
DEALLOCATE cur;--释放指针

 

posted @ 2022-01-09 21:03  Seasons1987  阅读(98)  评论(0编辑  收藏  举报