Errors in SQL SERVER triggers

Source: Internet
Author: User

Errors in SQL SERVER triggers

There is an after insert trigger that triggers several times when the same transaction inserts more than one data. How many records are there in the inserted staging table?

Myth: Within the same transaction, there must be only one trigger, insert a few according to the inserted table there are several

The actual situation is not true, to divide the situation, look at the INSERT INTO statement in the transaction how to write

Situation one.

BEGIN TRAN I

Nsert into T1 (field1,field2) VALUE (v001,v002);

INSERT into T1 (field1,field2) VALUE (v001,v002);

COMMIT

Result: Triggered two times, the inserted table has only one record

Situation two.

BEGIN TRAN

INSERT into T1 (field1,field2) VALUES (v101,v102), (v201,v202);

COMMIT

Result: Trigger once, inserted table has two records

Situation three.

BEGIN TRAN

INSERT into T2 SELECT ID, name, address from T1

COMMIT

Result: Trigger once, inserted table has multiple records

Conclusion: The number of triggers is independent of the same transaction and is related to multiple inser into directives

Errors in SQL SERVER triggers

Related Article

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.