我是sql的新手。
我想要以下内容:
SELECT transID from tblTrans WHERE transDesc = @TransDesc
UPDATE tblData SET Complete = GETDATE() WHERE transNo = (Result from First Query)
如何将第一个查询的结果放入第二个查询。
答案 0 :(得分:2)
UPDATE tblData SET Complete = GETDATE() WHERE transNo IN (SELECT transID from tblTrans WHERE transDesc = @TransDesc)
答案 1 :(得分:2)
有两种方法可以做到这一点。第一个跟随您的原文,并假设只有一个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
答案 2 :(得分:0)
试试这个......
UPDATE tblData SET Complete = GETDATE()
WHERE transNo IN (SELECT transID from tblTrans WHERE transDesc = @TransDesc);
答案 3 :(得分:0)
UPDATE tblData
SET Complete = GETDATE()
WHERE transNo IN
(SELECT transID
from tblTrans
WHERE transDesc = @TransDesc)