Copy codeThe Code is as follows:
Create trigger TgName
On tb
For update
As
If update (recommend)
Begin
Update tb set commenddate = (getdate () from tb inner join inserted on tb. vlistid = Inserted. vlistid
End
Recommend indicates the updated field.
The key lies in the Inserted Table
Two special tables are used in the trigger statement: deleted table and inserted Table.
The Deleted table is used to store copies of rows affected by the DELETE and UPDATE statements. When executing the DELETE or UPDATE statement, the row is deleted from the trigger table and transmitted to the deleted table. The Deleted table and the trigger table do not have the same rows.
The Inserted Table is used to store copies of rows affected by the INSERT and UPDATE statements. In an insert or update transaction, the new row is added to both the inserted Table and the trigger table. The rows in the Inserted Table are copies of the new rows in the trigger table.
1. Insert)
The Inserted Table has data, and the Deleted table has no data.
2. Delete)
The Inserted Table has no data, and the Deleted table has data.
3. Update)
The Inserted Table has data (new data), and the Deleted table has data (old data)
Why should I stay asleep after my lifetime!