0

I have a line:

Update <Table> Set <Value> = @NewValue Where RecordID = @RecordID;

However, now I find myself needing to update another table with the change in this value. Obviously I could do a separate query to get the original, or I could save the original when I originally read it. The elegant answer would be to somehow return it, though.

(RecordID is unique, this won't return multiple values.)

2
  • 1
    Possible duplicate of Get OLD Value in MySQL Trigger AFTER Update Statement Commented Jul 19, 2018 at 1:04
  • @SamM That's talking about triggers which aren't going to work in my case--the second update(s) require access to three fields, one of which is found in this record, the others are defined in a config and their values are in yet another table. Commented Jul 19, 2018 at 1:28

1 Answer 1

0

You can update both tables in one query

UPDATE t1
CROSS JOIN t2
SET t2.<value> = t1.<value>,
    t1.<value> = @NewValue
WHERE t1.RecordID = @RecordID1
AND t2.RecordID = @RecordID2

If the rows of the two tables are related, use an INNER JOIN instead of CROSS JOIN, and put the t2 condition there.

Sign up to request clarification or add additional context in comments.

2 Comments

There almost certainly are two records in t2 that need updating (and by the difference, not merely the value I'm writing) and that's simply current usage, a change to a configuration file could change the number of records affected.
You can change the t2.RecordID = @RecordID2 to any condition you want to find the affected rows.

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.