If you need to update data of a table (DestinationTable) with data of another table (SourceTable) there are 3 ways you can get this done in SQL.
Method 1
This is the most common and simple way with using a sub query.
- UPDATE DestinationTable SET DestinationColumn =
- (SELECT ValueColumn FROM SourceTable WHERE DestinationTable.MappingColumn = SourceTable.MappingColumn)
Method 2
This users the most common FROM clause to join the two tables as shown below.
- UPDATE DestinationTable SET DestinationColumn = SourceTable.ValueColumn
- FROM SourceTable
- WHERE DestinationTable.MappingColumn = SourceTable.MappingColumn
Method 3
Last method mentioned below uses the join clause to join both the tables to make the update happen properly.
- UPDATE DestinationTable SET DestinationColumn = SourceTable.ValueColumn
- FROM DestinationTable
- INNER JOIN SourceTable ON DestinationTable.MappingColumn = SourceTable.MappingColumn
Hope these helps.
No comments:
Post a Comment