Suppose I have two tables Main and Other like the following:
Main
+----------+-----------------------------------+
| Field1 | <set of other columns...> |
+----------+-----------------------------------+
| NULL | ... |
| NULL | ... |
| NULL | ... |
Other
+-----------------------------------+
| <same set of other columns...> |
+-----------------------------------+
| ... |
| ... |
| ... |
Is there a concise way to update Main.Field1 where the rest of the columns, taken together, are not in a row of Other?
In other words, I want to update Field1 for each row in
SELECT <set of other columns...> FROM Main
EXCEPT
SELECT <same set of other columns...> FROM Other
Dynamic SQL is an option, but I’m trying to figure out the most efficient way to do something like this.
If you really wanted to you could use the Except clause
DEMO
You should note that the use of
*here is very fragile and you should explicitly set your field list. It also assumes that the joining fields (PK) are available in both Main and Other and they would be the sameOther wise you’re much better off using NOT EXISTS or an ANTI-JOIN (LEFT/ISNULL)
DEMO