Your approach isn't the right approach. You should just use a default value for DateAdded
. But, the problem with this query:
UPDATE Products
SET DateAdded = GETDATE()
WHERE DateAdded IS NULL
AND DateAdded IN (SELECT DateAdded FROM inserted);
Is that the two conditions are incompatible. You have DataAdded IS NULL
fine. That is true. But almost any comparison to NULL
is false, including the IN
comparison. So for what you want to do, this should suffice:
UPDATE Products
SET DateAdded = GETDATE()
WHERE DateAdded IS NULL;
You could write:
UPDATE Products
SET DateAdded = GETDATE()
WHERE DateAdded IS NULL OR
DateAdded IN (SELECT DateAdded FROM inserted);
I do note that the trigger is updating the same table being modified, which often leads to confusing code.