Export (0) Print
Expand All
Expand Minimize

sp_settriggerorder (Transact-SQL)

Updated: 12 December 2006

Specifies the AFTER triggers that are fired first or last. The AFTER triggers that are fired between the first and last triggers are executed in undefined order.

Topic link icon Transact-SQL Syntax Conventions


sp_settriggerorder [ @triggername = ] '[ triggerschema. ] triggername' 
        , [ @order = ] 'value' 
        , [ @stmttype = ] 'statement_type' 
        [ , [ @namespace = ] { 'DATABASE' | 'SERVER' | NULL } ]

[ @triggername = ] '[ triggerschema. ] triggername'

Is the name of the trigger and the schema to which it belongs, if applicable, whose order is to be set or changed. [triggerschema.]triggername is sysname. If the name does not correspond to a trigger or if the name corresponds to an INSTEAD OF trigger, the procedure returns an error. triggerschema cannot be specified for DDL or logon triggers.

[ @order = ] 'value'

Is the setting for the new order of the trigger. value is varchar(10) and it can be any one of the following values.

ms186762.note(en-US,SQL.90).gifImportant:
The First and Last triggers must be two different triggers.

Value Description

First

Trigger is fired first.

Last

Trigger is fired last.

None

Trigger is fired in undefined order.

[ @stmttype = ] 'statement_type'

Specifies the SQL statement that fires the trigger. statement_type is varchar(50) and can be INSERT, UPDATE, DELETE, LOGON, or any Transact-SQL statement event listed in DDL Events for Use with DDL Triggers. Event groups cannot be specified.

A trigger can be designated as the First or Last trigger for a statement type only after that trigger has been defined as a trigger for that statement type. For example, trigger TR1 can be designated First for INSERT on table T1 if TR1 is defined as an INSERT trigger. The SQL Server 2005 Database Engine returns an error if TR1, which has been defined only as an INSERT trigger, is set as a First, or Last, trigger for an UPDATE statement. For more information, see the Remarks section.

@namespace = { 'DATABASE' | 'SERVER' | NULL }

When triggername is a DDL or logon trigger, specifies whether triggername was created with database scope or server scope. Note that logon triggers can be created only with server scope. For more information about DDL trigger scope, see Designing DDL Triggers. If not specified, or if NULL is specified, triggername is a DML trigger.

0 (success) and 1 (failure)

There can be only one First and one Last trigger for each statement on a single table.

If a First trigger is already defined on the table, database, or server, you cannot designate a new trigger as First for the same table, database, or server for the same statement_type. This restriction also applies Last triggers.

Replication automatically generates a first trigger for any table that is included in an immediate updating or queued updating subscription. Replication requires that its trigger be the first trigger. Replication raises an error when you try to include a table with a first trigger in an immediate updating or queued updating subscription. If you try to make a trigger a first trigger after a table has been included in a subscription, sp_settriggerorder returns an error. If you use ALTER TRIGGER on the replication trigger, or use sp_settriggerorder to change the replication trigger to a Last or None trigger, the subscription does not function correctly.

If an ALTER TRIGGER statement changes a first or last trigger, the First or Last attribute originally set on the trigger is dropped, and the value is replaced by None. The order value must be reset by using sp_settriggerorder.

If the same trigger must be designated as the first or last order for more than one statement type, sp_settriggerorder must be executed for each statement type. Also, the trigger must be first defined for a statement type before it can be designated as the First or Last trigger to fire for that statement type.

To set the order of a DDL or logon trigger with server scope (created ON ALL SERVER), requires CONTROL SERVER permission.

To set the order of a DDL trigger with database scope (created ON DATABASE), requires ALTER ANY DATABASE DDL TRIGGER permission.

To set the order of a DML trigger, requires ALTER permission on the table or view on which the trigger is defined.

A. Setting the firing order for a DML trigger

The following example specifies that trigger uSalesOrderHeader be the first trigger to fire after an UPDATE operation occurs on the Sales.SalesOrderHeader table.

USE AdventureWorks;
GO
sp_settriggerorder @triggername= 'Sales.uSalesOrderHeader', @order='First', @stmttype = 'UPDATE';

B. Setting the firing order for a DDL trigger

The following example specifies that trigger ddlDatabaseTriggerLog be the first trigger to fire after an ALTER_TABLE event occurs in the AdventureWorks database.

USE AdventureWorks;
GO
sp_settriggerorder @triggername= 'ddlDatabaseTriggerLog', @order='First', @stmttype = 'ALTER_TABLE', @namespace = 'DATABASE';

Release History

12 December 2006

New content:
  • Added information throughout the topic about logon triggers, which are introduced in SQL Server 2005 Service Pack 2.

Community Additions

ADD
Show:
© 2014 Microsoft