开发者

SQL Server trigger execution

开发者 https://www.devze.com 2023-02-02 22:26 出处:网络
Say I have an UPDATE trigger on tableA that inserts a new record into tableB. CREATE TRIGGER insertIntoTableB

Say I have an UPDATE trigger on tableA that inserts a new record into tableB.

CREATE TRIGGER insertIntoTableB
ON tableA
FOR UPDATE 
AS
   INSERT INTO tableB (...) VALUES (...)  
GO

I then run these statements sequentially. Will the second UPDATE statement (UPDATE tableB) work OK? (i.e. wait for the trigger on table A to fully execute)

UPDATE tableA
SET ...
WHERE key = 'some key'

UPDATE tableB
SET ...
WHERE key = 开发者_JS百科'newly inserted key from trigger'


The behavior is subject to the nested triggers server configuration, see Using Nested Triggers:

Both DML and DDL triggers are nested when a trigger performs an action that initiates another trigger. These actions can initiate other triggers, and so on. DML and DDL triggers can be nested up to 32 levels. You can control whether AFTER triggers can be nested through the nested triggers server configuration option. INSTEAD OF triggers (only DML triggers can be INSTEAD OF triggers) can be nested regardless of this setting.

When a trigger on table A fires and inside the trigger table B is updated, the trigger on table B runs immediately. The Table A trigger did not finish, it is blocked in waiting for the UPDATE statement to finish, which in turn waits for the Table B trigger to finish. However, the updates to table A have already occurred (assuming a normal AFTER trigger) and querying the Table A from the table B's trigger will see the updates.


If the updates are sequentially coded into the UPDATE trigger of A then yes.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号