update table with sum from another table

yesterday, I had a problem to update table with sum of data in another table using sqlserver. finally after I search in google, I found a simple way like this :

update C
Set C.qty = A.jum
from C
Inner Join (
Select SUM(cast(piece as int)) As jum,
part_no
From part_list
Group By part_no
) As A
on A.part_no = C.part_no

and It’s perfectly works.

but, if you just need to update table with data from another table, you can used this simple code

update t1
set col2 = t2.col2
from table1 t1 inner join table2 t2
on t1.col1=t1.col2
where Col3 >5

good luck !!

Advertisements