Inside a trigger, I want to not only get the previous values of the rows that were changed (DELETED table) but also the values of the rows that weren't changed at all.
Would this work?
SELECT * FROM DELETED
UNION
SELECT * FROM Table WHERE Table.Id NOT IN (SELECT Id FROM DELETED)
1条答案
按热度按时间ecr0jaav1#
You say
I'm trying to compare how many rows there are in the table before the trigger and after the trigger
You don't actually need to query the whole table, which is very inefficient. You can just compare
inserted
anddeleted
This works in a trigger which handles all of
INSERT, UPDATE, DELETE
.If you have just a
INSERT
orUPDATE
trigger you only need to queryinserted
.If you have just a
DELETE
trigger you only need to querydeleted
.If you need the full amouont of rows in the table, the fastest (if not always perfectly accurate) way of doing it is:
This result is after changes have been applied, so subtract the previous result to get the before value.