我对 sql 很陌生。
我想要以下内容:
SELECT transID from tblTrans WHERE transDesc = @TransDesc
UPDATE tblData SET Complete = GETDATE() WHERE transNo = (Result from First Query)
如何将第一个查询的结果放入第二个查询。
我对 sql 很陌生。
我想要以下内容:
SELECT transID from tblTrans WHERE transDesc = @TransDesc
UPDATE tblData SET Complete = GETDATE() WHERE transNo = (Result from First Query)
如何将第一个查询的结果放入第二个查询。
UPDATE tblData SET Complete = GETDATE() WHERE transNo IN (SELECT transID from tblTrans WHERE transDesc = @TransDesc)
有两种方法可以做到这一点。第一个遵循您的原件,并假设只有一个 transID:
DECLARE @transID int
SELECT @transID = transID from tblTrans WHERE transDesc = @TransDesc
UPDATE tblData SET Complete = GETDATE() WHERE transNo = @transID
第二个更简洁,因为它将整个更新放入一个命令中:
UPDATE tblData SET Complete = GETDATE()
FROM tblTrans t
WHERE t.transID = tblData.transNo
AND t.transDesc = @TransDesc
尝试这个...
UPDATE tblData SET Complete = GETDATE()
WHERE transNo IN (SELECT transID from tblTrans WHERE transDesc = @TransDesc);
UPDATE tblData
SET Complete = GETDATE()
WHERE transNo IN
(SELECT transID
from tblTrans
WHERE transDesc = @TransDesc)