Thursday, January 10, 2013

Update data in one table with data from another table in MS SQL Server

How to update the data from different table

Here is Sql Server Update statement

Update DestinationTable
Set Field = SourceTable.Field  //if you have multiple field separated by comma
From DestinationTable
Inner Join SourceTable
On DestinationTable.CommonField = SourceTable.CommonField

Below live example of update of sql server

Destination Table 

ProudctID Name ProudctPrice
100 Glasses $29.99
101 Lens $99.99

Source Table 

ProudctID Name ProudctPrice
100 Glasses $49.99
101 Lens $199.99

Update DestinationTable
Set ProductPrice= SourceTable.ProductPrice //if you have multiple field separated by comma
From DestinationTable
Inner Join SourceTable
On DestinationTable.ProductID= SourceTable.ProductID

Result after the update

Destination Table 

ProudctID Name ProudctPrice
100 Glasses $49.99
101 Lens $199.99

No comments :