This article introduces mssqlserver to determine whether a trigger is processing an insert, delete, or update trigger. If you need to learn more, refer to.
This article introduces how to determine whether a trigger is processing an insert, delete, or update trigger by mssql server. If you need to learn more, refer to the introduction section.
| The Code is as follows: |
|
|
-- Declare two variables
DECLARE @ d bit = 0
DECLARE @ I BIT = 0
-- If a record is found in the temporary trigger table of DELETED, the old data is DELETED.
If exists (select top 1 1 from deleted)
SET @ D = 1
-- If a record is found in the temporary trigger table of INSERTED, new data is INSERTED.
If exists (select top 1 1 from inserted)
SET @ I = 1
-- If both tables have records, the update is triggered.
IF @ I = 1 AND @ D = 1
PRINT (n' update. ')
-- If the variable @ I is changed to 1, and the variable @ D is not changed, the trigger is triggered by executing the insert operation.
IF @ I = 1 AND @ D = 0
PRINT (n'insert ')
-- The following judgment is true, indicating that the trigger is triggered when the deletion is executed.
IF @ I = 0 AND @ D = 1
PRINT (n'delete ') |