How can I do an UPDATE statement with JOIN in SQL Server, based on an Id match?
Richard C.
—In SQL Server, how do you update rows in one table with data from rows in another? You might want to join two tables in your update, where one table contains more recent or authoritative data than the other. The tables often share an Id
or some type of account number that you can match your records against.
Assume you have two tables. One called Person
that is kept up to date with correct details. The other table is called PersonArchive
and is updated monthly with recent details from Person
.
CREATE TABLE Person ( Id INT PRIMARY KEY, Name VARCHAR(255) ); CREATE TABLE PersonArchive ( Id INT PRIMARY KEY, Name VARCHAR(255) ); INSERT INTO Person(Id, Name) VALUES (1, 'Amir'), (2, 'Sofia'), (3, 'Aya'), (4, 'Mateo'), (5, 'Leila'), (6, 'Yara'), (7, 'Ndidi'), (8, 'Santiago'); INSERT INTO PersonArchive(Id, Name) VALUES (1, 'Abir'), (2, 'Sofya'), (3, 'Aya'), (4, 'Mateo'), (5, 'Leila'), (6, 'Yara'), (7, 'Ndidi'), (8, 'Santiago');
Below is an update query that uses a join to update the archive table from the reference table:
UPDATE old SET old.Name = new.Name FROM PersonArchive old JOIN Person new ON old.id = new.id;
To make the query more readable you could use a WHERE
clause instead of a join:
UPDATE old SET old.Name = new.Name FROM PersonArchive old, Person new WHERE old.id = new.id;
The queries above are likely to be fast. However, they will work only in SQL Server. Each database server’s update syntax is slightly different.
Instead, you can update your table using a subquery, which will work in all database servers, but might run more slowly on large tables:
UPDATE PersonArchive SET Name = ( SELECT Name FROM Person WHERE PersonArchive.Id = Person.Id );
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at [email protected].
If you are a California resident, see our Supplemental notice.