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,
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 !!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s