sql 递减计算

CREATE TABLE #temp
    (
      qty INT ,
      qty1 INT ,
      qty2 INT
    );

INSERT  INTO #temp
        ( qty, qty1, qty2 )
VALUES  ( 7000, 0, 0 ),
        ( 6000, 0, 0 ),
        ( 5000, 0, 0 ),
        ( 4000, 0, 0 );

DECLARE @p INT = 15000;

UPDATE  #temp
SET    @p = @p - qty,qty1 = CASE WHEN @p>0 THEN qty WHEN qty+@p>0 THEN qty+@p ELSE 0 END,qty2=@p

SELECT  *
FROM    #temp;

DROP TABLE #temp;

  

posted @ 2023-08-08 11:04  瓜甜  阅读(81)  评论(0编辑  收藏  举报