SQL UPDATE triggers can get information about the rows being update _mssql

Source: Internet
Author: User
Copy Code code 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 represents the field being updated.
The key is the inserted table
Two special tables are used in trigger statements: Deleted tables and inserted tables.
The Deleted table is used to store a copy of the rows affected by the DELETE and UPDATE statements. When the delete or UPDATE statement is executed, the row is removed from the trigger table and transferred to the deleted table. Deleted tables and trigger tables usually do not have the same rows.

The Inserted table is used to store a copy of the rows affected by the INSERT and UPDATE statements. In one insert or update transaction, new rows are added to both the inserted table and the trigger table. The row in the Inserted table is a copy of the new row in the trigger table.

1. Inserting operation (insert)
Inserted table has data, deleted table has no data

2. Delete operation (delete)
Inserted table has no data, deleted table has data

3. Update operation (updated)
Inserted table has data (new data), deleted table has data (old data)

Why do you sleep before you die?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.