DECLARE @nmSupport NUMERIC(14,4),@nmOutst NUMERIC(14,4),@chPart VARCHAR(16) SELECT @chPart = 'XXX' SELECT @nmOutst = 0 SELECT @nmSupport = 0 UPDATE acdb.PAC06.AC06004A SET nmSupport = CASE WHEN chPart = @chPart THEN CASE WHEN nmQty > @nmOutst THEN @nmOutst ELSE nmQty END ELSE CASE WHEN nmQty > nmOutst THEN nmOutst ELSE nmQty END END, nmOutst = CASE WHEN chPart = @chPart then case when @nmOutst>=@nmSupport then @nmOutst-@nmSupport else 0 end else nmOutst-nmSupport END, @nmOutst = CASE WHEN chPart = @chPart then case when @nmOutst>=@nmSupport then @nmOutst-@nmSupport else 0 end else nmOutst-nmSupport END, @nmSupport = case when chPart = @chPart then case when nmQty > @nmOutst then @nmOutst else nmQty end ELSE CASE WHEN nmQty > nmOutst THEN nmOutst ELSE nmQty END end, @chPart = chPart改成用SQLSERVE遍历的方式
时间: 2024-03-11 10:47:18 浏览: 54
可以使用 WHILE 循环遍历实现相同的功能,以下是使用 WHILE 循环的代码示例:
```
DECLARE @nmSupport NUMERIC(14,4),@nmOutst NUMERIC(14,4),@chPart VARCHAR(16)
DECLARE @rowcount INT, @counter INT
SELECT @counter = 1
SELECT @rowcount = COUNT(*) FROM acdb.PAC06.AC06004A
WHILE @counter <= @rowcount
BEGIN
SELECT @chPart = chPart, @nmQty = nmQty, @nmOutst = nmOutst, @nmSupport = nmSupport
FROM (SELECT ROW_NUMBER() OVER (ORDER BY chPart) AS RowNum, chPart, nmQty, nmOutst, nmSupport FROM acdb.PAC06.AC06004A) AS RowConstrainedResult
WHERE RowNum = @counter
IF @chPart = 'XXX'
BEGIN
SET @nmSupport = CASE WHEN @nmQty > @nmOutst THEN @nmOutst ELSE @nmQty END
END
ELSE
BEGIN
SET @nmSupport = CASE WHEN @nmQty > @nmOutst THEN @nmOutst ELSE @nmQty END
END
UPDATE acdb.PAC06.AC06004A
SET nmSupport = @nmSupport,
nmOutst = CASE WHEN chPart = @chPart then case when @nmOutst>=@nmSupport then @nmOutst-@nmSupport else 0 end else nmOutst-@nmSupport END,
@nmOutst = CASE WHEN chPart = @chPart then case when @nmOutst>=@nmSupport then @nmOutst-@nmSupport else 0 end else nmOutst-@nmSupport END
WHERE chPart = @chPart
SET @counter = @counter + 1
END
```
请注意,使用 WHILE 循环遍历时,需要先获取表中的行数,如果行数很大,可能会对性能产生影响。
阅读全文