Update Table Column Value from another Table (Optional Way of Inner Join)

Update Table Column Value from another Table (Optional Way of Inner Join)

Yesterday, i was working on sql Store procedure to update table data from other table, and Inner Join is best way to update from another table.

but there is too many raw so i was confused in use of inner join.

My senior teach me another way of updating column value.
  
Table1 DiamondMaster

id     | Shape | Color  | Lab | Price
-----------------------------------
1001 | Round | White | GIA | 50000
1002 | Heart  | Red   | GIA  | 80000
1003 | Round | White | IGI  | 50000
 
Table2 DiamondNewPrice

id     | NewPrice
---------------
1002 | 75000
1003 | 55000
 
1. First Way Using Inner Join
 
UPDATE DiamondMaster

SET DiamondMaster.Price = DiamondNewPrice.NewPrice
FROM DiamondMaster INNER JOIN DiamondNewPrice On DiamondMaster.id = DiamondNewPrice.id
 
2. Other Way
 
UPDATE DiamondMaster

SET DiamondMaster.Price = i.NewPrice FROM (select id, NewPrice from DiamondNewPrice) i where i.id = DiamondMaster.id

-------
Thanks
Ebook Download
View all
Learn
View all