Mssqlserver determines whether the trigger is processing insert, delete, or update.

Source: Internet
Author: User
Tags mssql server mssqlserver
This article introduces mssqlserver to determine whether a trigger is processing an insert, delete, or update trigger. If you need to learn more, refer to.

This article introduces how to determine whether a trigger is processing an insert, delete, or update trigger by mssql server. If you need to learn more, refer to the introduction section.

The Code is as follows:

-- Declare two variables
DECLARE @ d bit = 0
DECLARE @ I BIT = 0

-- If a record is found in the temporary trigger table of DELETED, the old data is DELETED.
If exists (select top 1 1 from deleted)
SET @ D = 1

-- If a record is found in the temporary trigger table of INSERTED, new data is INSERTED.
If exists (select top 1 1 from inserted)
SET @ I = 1

-- If both tables have records, the update is triggered.
IF @ I = 1 AND @ D = 1
PRINT (n' update. ')

-- If the variable @ I is changed to 1, and the variable @ D is not changed, the trigger is triggered by executing the insert operation.
IF @ I = 1 AND @ D = 0
PRINT (n'insert ')

-- The following judgment is true, indicating that the trigger is triggered when the deletion is executed.
IF @ I = 0 AND @ D = 1
PRINT (n'delete ')

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.