0

I am pretty new at sql.

I want the following:

SELECT transID from tblTrans WHERE transDesc = @TransDesc

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

How do I put the result of the first query into the 2nd query.

4 Answers 4

2
UPDATE tblData SET Complete = GETDATE() WHERE transNo IN  (SELECT transID from tblTrans WHERE transDesc = @TransDesc)
Sign up to request clarification or add additional context in comments.

Comments

2

There are two ways of doing this. The first follows your original, and assumes there is only one transID:

DECLARE @transID int

SELECT @transID = transID from tblTrans WHERE transDesc = @TransDesc

UPDATE tblData SET Complete = GETDATE() WHERE transNo = @transID

The second is neater, as it puts the entire update into a single command:

UPDATE tblData SET Complete = GETDATE()
    FROM tblTrans t
    WHERE t.transID = tblData.transNo
      AND t.transDesc = @TransDesc

Comments

0
UPDATE tblData 
SET Complete = GETDATE() 
WHERE transNo IN
    (SELECT transID 
     from tblTrans 
    WHERE transDesc = @TransDesc)

Comments

0

Try this...

UPDATE tblData SET Complete = GETDATE()
    WHERE transNo IN (SELECT transID from tblTrans WHERE transDesc = @TransDesc);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.