Trigger --- for INSERT and INSTEAD OF

zhaozj2021-02-16  43

Note:

The following example is provided by the 9CBS Forum MS-SQL Server Section, INTERNET9CBS (August 10, 1979)

Test one.

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

Create Table TB1 (ID Int Id IDENTITY (1, 1), A varchar (10))

Create Trigger Tri_TB1 on TB1 INSTEAD OFSERT

AS

Begin

INSERT TB1 (a)

SELECT A from Inserted

end

INSERT TB1 (a)

SELECT 'A'

SELECT * from TB1

/ *

ID A

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

1 a

* /

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

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

Test one.

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

Create Table TB1 (ID Int Id IDENTITY (1, 1), A varchar (10))

Create Trigger Tri_TB1 on Tb1 for Insert

AS

Begin

INSERT TB1 (a)

SELECT A from Inserted

end

INSERT TB1 (a)

SELECT 'A'

SELECT * from TB1

/ *

ID A

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

1 a

2 a

* /

============================================================================================================================================================================================================= =========

Conclusion: for insert, the trigger statement is executed after the insertion action is completed.

INDSTEAD OF INSERT, with trigger statements instead of inserted statements.

After

Specifies that the trigger is only excited after all the operations specified in the SQL statement have been successfully executed. All reference cascade operations and constraints must also be executed after successful completion.

If only the for keyword is specified, the AFTER is the default setting.

INSTEAD OF

Specifies that the trigger is executed instead of performing a trigger SQL statement, replacing the trigger statement.

转载请注明原文地址:https://www.9cbs.com/read-20718.html

New Post(0)