SQL UPDATE trigger to get information about the row being update

Source: Internet
Author: User

Type: Reprint

The SQL UPDATE trigger can get information about the row being update, and the friend you need can refer to it. 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 represents the field being updated.
The key is the inserted table
Two special tables are used in trigger statements: the deleted table and the inserted table.
The Deleted table is used to store copies of the rows affected by the DELETE and UPDATE statements. When you execute a delete or UPDATE statement, 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 an INSERT or update transaction, the new row is 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. Insertion operation (insert)
Inserted table with data, deleted table no data

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

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

SQL UPDATE trigger to get information about the row being update

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.