Skip to content
Advertisement

Performing an UPDATE with Union in SQL

If I were to have these three tables (just an example in order to learn UNION, these are not real tables):

Tables with their columns:

And I wanted to update order_status in the Customer table when there is a order filled in either the Order_Web table or the Order table for that customer using Union:

How can I combine these two updates using a Union on order_web and order?

Using Microsoft SQL Server Management Studio

Advertisement

Answer

You do not need a UNION for that – replacing an inner join with a pair of outer ones should do it:

You could also use a WHERE EXISTS, like this:

If you must use UNION, you can do it as follows:

or the same one with a JOIN:

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