Wikipedia:Reference desk/Archives/Computing/2021 October 24

= October 24 =

Question about updating rows in table in Microsoft SQL Server
I have run into a rather interesting database scenario at work. We are using Microsoft SQL Server as a database.

There are two tables, let's call them  and. Both of them have  and   columns. In each table,  is unique, so it has no duplicate rows inside the table. On the other hand, the  values are shared across the tables, because the whole point is to link rows between the two tables.

Now each table also has a unique  value for each   value. The thing is, the  values in   are wrong, and I want to update them to be the same as in.

For a single row, this can be done with something like this:. But is there a way to do this for multiple rows, other than writing the same update for each row separately? J I P &#124; Talk 15:30, 24 October 2021 (UTC)


 * Yes. I believe you can reference link the IDs in the subselect like: . This is referred to as a correlated subquery.
 * Another approach is to use a join like
 * The only difference would be for a case where TableA does not have a record matching TableB. On a side note, it usually not a good practice to store duplicate data across tables for precisely the data synchronization issues you have encountered here. See database normalization. If you find a need to combine data together in one place for convenience and ease of access, you might want to consider using a database view. -- Tom N  talk/contrib 20:57, 24 October 2021 (UTC)
 * The only difference would be for a case where TableA does not have a record matching TableB. On a side note, it usually not a good practice to store duplicate data across tables for precisely the data synchronization issues you have encountered here. See database normalization. If you find a need to combine data together in one place for convenience and ease of access, you might want to consider using a database view. -- Tom N  talk/contrib 20:57, 24 October 2021 (UTC)