Update table from another

I needed to add two more fields to a table. I need to make the update of these two columns with the corresponding fields from the other table. I have a common key to both tables. However there are some records in a table that does not exist in the other, and I would leave them to zero.
How can make update query.
Example.jpg
rflorencioAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tony303Commented:
What are the Vat and Wvat values for record 1236?
Blank or NULL....it makes a difference.
0
Tony303Commented:
Do on a Test System first....

Update TableB
SET TableB.Vat = TableA.Vat
FROM TableA, TableB
WHERE TableA.Stamp = TableB.Stamp

This next script updates the rest of TableB

Update TableB
SET Vat = 0, Wvat = 0
Where Stamp Not IN (SELECT STamp FROM TABLEA)
0
rflorencioAuthor Commented:
Null values
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

tigin44Commented:
UPDATE A
SET A.colx= ISNULL(B.colx,0), A.coly = ISNULL(B.coly, 0)
FROM tableA A
   LEFT OUTER JOIN tableB B ON A.id= B.id
0
Tony303Commented:
@rfloencio

Mine will work, try in a test system first.

Tony
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
tigin44,

The tables in your update are reversed. TableB should receive the updates. And you should use the correct column names whenever they are provided:
UPDATE B
SET B.Vat= ISNULL(A.Vat, 0),
    B.WVat = ISNULL(A.WVat, 0)
FROM tableB B
LEFT JOIN tableA A ON A.Stamp= B.Stamp

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.