0

我对 sql 很陌生。

我想要以下内容:

SELECT transID from tblTrans WHERE transDesc = @TransDesc

UPDATE tblData SET Complete = GETDATE() WHERE transNo = (Result from First Query)

如何将第一个查询的结果放入第二个查询。

4

4 回答 4

2
UPDATE tblData SET Complete = GETDATE() WHERE transNo IN  (SELECT transID from tblTrans WHERE transDesc = @TransDesc)
于 2013-06-28T15:14:23.027 回答
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
于 2013-06-28T15:16:17.650 回答
0

尝试这个...

UPDATE tblData SET Complete = GETDATE()
    WHERE transNo IN (SELECT transID from tblTrans WHERE transDesc = @TransDesc);
于 2013-06-28T15:14:09.403 回答
0
UPDATE tblData 
SET Complete = GETDATE() 
WHERE transNo IN
    (SELECT transID 
     from tblTrans 
    WHERE transDesc = @TransDesc)
于 2013-06-28T15:15:08.443 回答