This note is in Hasura.Backends.MSSQL.DDL.EventTrigger. It is referenced at:
An MS-SQL trigger is different from a postgres trigger in some ways
inserted
and deleted
.
The rows in the inserted
table are copies of the new rows in the trigger
table and similarly the deleted
table contains the copies of the rows
that were deleted from the trigger table.deleted
table and the new data will be copied to the
inserted
table.Since we deliver the ‘old’ and ‘new’ data in the event trigger payload, we would need
a way to correlate the values from inserted
and deleted
tables. And this is why,
It is mandatory for a MSSQL Update trigger table to have a primary key. We use this
primary key to correlate between inserted
and deleted
MSSQL UPDATE trigger’s join clause depends on the fact that the primary key is never updated. But when the primary key is updated, you cannot join the ‘INSERTED’ and the ‘DELETED’ tables. Hence for those cases, we consider the old payload as NULL and the new payload will contain the updated row changes.
To figure out if a primary key has been updated, we do the following: For each row present in the INSERTED table, we check if there are any rows in DELETED tabled that has the same primary key as that of the row in INSERTED table. If such a row does not exists, then it means that the primary key has been updated. The sample SQL which does this looks like: SELECT * FROM INSERTED WHERE NOT EXISTS (SELECT * FROM DELETED WHERE INSERTED.id = DELETED.id )
The spec for MSSQL UPDATE Event Trigger is as follows: