SQL Server trigger temporary table

Source: Internet
Author: User
In SQL triggers, the system automatically generates two temporary tables, deleted and inserted, which are logical (concept) tables. It can also be understood as a temporary table. For ease of understanding, we can consider the role of the Inserted Table and the Deleted table as follows: the Inserted Table is a new record, and the Delete table is an old record. And their table structure

In SQL triggers, the system automatically generates two temporary tables, deleted and inserted, which are logical (concept) tables. It can also be understood as a temporary table. For ease of understanding, we can consider the role of the Inserted Table and the Deleted table as follows: the Inserted Table is a new record, and the Delete table is an old record. And their table structure

SQLTriggerThe system automatically generates twoTemporaryTables: deleted and inserted. They are logical (concept) tables. It can also be understoodTemporaryTable.

For ease of understanding, we can consider the role of the Inserted Table and the Deleted table as follows:An Inserted Table is a new record, and a Delete table is an old record.In addition, their table structure is exactly the same as that of the original table.

1. When you Insert, the record to be inserted is new, so it can be found in the Insert table.

For example, if N records exist in Table 1, but insert into table1 value ('1') is executedTriggerThe Inserted Table has only the same record.
Select * from Inserted, and the result is: 1 instead of N + 1 records of table 1.


2. When you update, the record to be updated is new, so you can find it in the Inserted Table. The updated record is old, so it can be found in the Deleted table.

For example, Change Record 1 to record 2.
The select * from Deleted result is 1 select * from Inserted result is 2



3. When you delete a record, the record to be Deleted is old, so you can find it in the Deleted table.

For example, if the table contains 1, 2, 3, and 4 records and three records are deleted, the result of select * from deleted is 1, 2, 3.

-------------------------

By the way, when you create a tableTriggerThen, we will discuss it in three cases.

  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)

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.