我编写了以下运行但未更新的代码。
UPDATE FINDOC
SET CCCPAPCUSCODE=
(
CASE
WHEN TRDBRANCH IS NULL THEN (SELECT TOP 1 CCCCODE1 FROM TRDBRANCH B WHERE B.TRDBRANCH=TRDBRANCH)
ELSE (SELECT TOP 1 CODE1 FROM TRDR T WHERE T.TRDR=TRDR) END
)
WHERE TRNDATE='20160226' AND
SERIES IN (1001,1021,1502,1522,1009)
有人可以提出原因吗?
答案 0 :(得分:1)
尝试使用类似的东西:
UPDATE FD
SET FD.CCCPAPCUSCODE = CASE
WHEN FD.TRDBRANCH IS NULL THEN (SELECT TOP 1 CCCCODE1 FROM TRDBRANCH B WHERE B.TRDBRANCH=FD.TRDBRANCH)
ELSE (SELECT TOP 1 CODE1 FROM TRDR T WHERE T.TRDR=FD.TRDR)
END
FROM FINDOC FD
WHERE FD.TRNDATE='20160226'
AND FD.SERIES IN (1001,1021,1502,1522,1009)
答案 1 :(得分:0)
你的逻辑似乎是错误的。使用@ Megartron的代码版本,当FD.TRDBRANCH
为NULL
时,子查询将不会返回任何记录,因此您的更新将不执行任何操作。您的意思是检查NOT NULL
吗?在这种情况下,正确的代码是:
UPDATE FD
SET FD.CCCPAPCUSCODE = CASE WHEN FD.TRDBRANCH IS NOT NULL
THEN ( SELECT TOP 1
CCCCODE1
FROM TRDBRANCH B
WHERE B.TRDBRANCH = FD.TRDBRANCH
)
ELSE ( SELECT TOP 1
CODE1
FROM TRDR T
WHERE T.TRDR = FD.TRDR
)
END
FROM FINDOC FD
WHERE FD.TRNDATE = '20160226'
AND FD.SERIES IN ( 1001, 1021, 1502, 1522, 1009 )