There are two special logical tables in the trigger:
Inserted and deleted
These two tables are automatically generated by the system.
Inserted records newly added or modified content.
The content recorded in deleted is the content before deletion or modification.
For example, see the following table:
A
ID, name
1 aa
2 bb
1. Add
Execute: insert into a (ID, name) values (3, 'cc ')
In this case
The content of the inserted Table is:
3 cc
The content of the deleted table is empty.
2. Modify
Run: update a set name = Name + '1' where ID <3
In this case
The content of the inserted Table is:
1 aa1
2 bb1
The content of the deleted table is empty.
1 aa
2 bb
3. Delete
Run: delete from a where id = 2
In this case
The content of the inserted Table is null.
The content of the deleted table is empty.
2 bb
Therefore, your trigger can be written as follows:
Method 1
Create trigger AA on your table
For insert, delete, update
If exists (select * From inserted)
If exists (select * From deleted)
...... -- Record the operation as a modified processing statement
Else
...... -- Record the operation as a new processing statement
Else
...... -- Record operation as a delete statement
Method 2. Separate write
Create trigger AA on your table
For insert
As
...... -- Record the operation as a new processing statement
Create trigger AA on your table
For update
As
...... -- Record the operation as a modified processing statement
Create trigger AA on your table
For Delete
As
...... -- Record the operation as a delete statement
Example:
Note:
1. The trigger runs after being inserted. If a primary key conflict occurs before, the trigger cannot be reached.
2. Automatic + 1 rules. When multiple users perform operations, they cannot ensure that the generated keywords are not repeated. What should I do when there are duplicate keywords?
3. The instead of insert trigger can avoid problems.