UPDATE ABC SET price = T1.price, qty = T1.qty FROM ABC INNER JOIN ABC_Temp T1 ON T1.item_no = ABC.item_no LEFT OUTER JOIN ABC_Temp T2 ON T2.item_no = T1.item_no AND T2.last_updated_date > T1.last_updated_date WHERE T2.item_no IS NULL AND ( T1.price <> ABC.price OR T1.qty <> ABC.qty )
If NULL values ββare possible in the price or qty columns, you will need to consider this. In this case, I would probably change the inequality operators like this:
COALESCE(T1.price, -1) <> COALESCE(ABC.price, -1)
This assumes that -1 is not a valid value in the data, so you don't have to worry about it really appearing there.
Also, is ABC_Temp really a temporary table that is just loaded long enough to get values ββin ABC? If not, then you save duplicate data in several places, which is a bad idea. The first problem is that now you need these update scripts. There are other problems that may arise, for example, inconsistencies in the data, etc.
Tom h source share