beta beta - 15 days ago 7
SQL Question

SQL Server Trigger: INSERTED and DELETED does not exist. What is it?

I need to debug (aka reverse engineer) a SQL Server trigger. The trigger is an

AFTER INSERT, UPDATE
trigger.

At the beginning of the trigger, there is the following
IF
clause:

if not exists(select 1 from inserted) and not exists(select 1 from deleted)
begin
print 'something'
return
end


The code runs into this
IF
and the text
something
is printed. I wonder, why this happens? I execute an
UPDATE
on the respective table which triggers the trigger. The
IF
basically becomes true, when it is NOT an insert and NOT a delete. Hence, I wonder why the
print
statement is executed, because an
UPDATE
essentially is a
DELETE
+
INSERT
.

Answer

The condition will be true only when no rows were modified by the UPDATE statement.