Skip to content
Advertisement

MS SQL Server Update or Clear based on matching values

I need some help in a scenario that I am working on.

I have two tables Product_Staging and Product. The contents of the table are below.

Product_Staging:

Product:

I need to update the Product table’s Cur_Revenue and Prev_Revenue fields with the contents from Product_Staging table when Account_No and Product_No matched, otherwise clear the values in Product table. Sample output is below.

Product After Update:

Row 15/DF from Product_Staging will be discarded as it does not exist in the Product table.

Can someone help?

Advertisement

Answer

You can try to use update .... OUTER JOIN

sqlfiddle

If you want to set a default value but the row does not exist in the Product table, you can try to use ISNULL function.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement