SQL Server - 저장 프로시저로 트리거 순서 설정 / Trigger Execution Order
서버 범위 트리거는 항상 데이터베이스 단위 트리거보다 먼저 실행된다.
Server scoped triggers will always fire before any of the database scoped triggers
그리고 sp_settriggerorder 라는 저장 프로시저를 통하여 실행 순서를 먼저 지정할 수 있다.
Using the sp_settriggerorder stored procedure, you can set the execution order of server-scoped or database-scoped triggers
Parameter | Description |
@triggername | Name of the trigger |
@order | Value can be First, Last or None. When set to None, trigger is fired in random order |
@stmttype | SQL statement that fires the trigger. Can be INSERT, UPDATE, DELETE or any DDL event |
@namespace | Scope of the trigger. Value can be DATABASE, SERVER, or NULL |
서버 범위, 데이터베이스 범위의 실행 순서 다 있는 경우 순서가 어떻게 되는지 알아보자
If you have a database-scoped and a server-scoped trigger handling the same event, and if you have set the execution order at both the levels. Here is the execution order of the triggers.
순서
1. First로 지정된 서버 범위의 트리거The server-scope trigger marked First
2. First나 Last로 지정되지 않은 서버 범위의 트리거 Other server-scope triggers
3. Last로 지정된 서버 범위의 트리거 The server-scope trigger marked Last
4. First로 지정된 데이터베이스 범위의 트리거 The database-scope trigger marked First
5. First나 Last로 지정되지 않은 데이터베이스 범위의 트리거 Other database-scope triggers
6. Last로 지정된 데이터베이스 범위의 트리거 The database-scope trigger marked Last