O'Reilly logo

Writing Stored Procedures for Microsoft SQL Server by Matthew Shepker

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating Triggers

Like all other objects in SQL Server, triggers are created through a CREATE statement. As you might suspect, this is actually a CREATE TRIGGER statement. The actual syntax for the CREATE TRIGGER statement is as follows:

CREATE TRIGGER trigger_name
ON {table | view}
[WITH ENCRYPTION]
{
{{FOR | AFTER | INSTEAD OF} { [DELETE] [,] [INSERT] [,] [UPDATE] }
    [WITH APPEND]
    [NOT FOR REPLICATION]
    AS
    { IF UPDATE (column)
    [{AND | OR}  UPDATE (column)]
    [...n]
    | IF (COLUMNS_UPDATED() {bitwise_operator} updated_bitmask)
    { comparison_operator} column_bitmask [...n]
    }
    sql_statement [...n]
}

Table 15.1 lists the syntax options available.

Table 15.1. Trigger Creation Syntax
Option Description
CREATE TRIGGER Tells SQL Server that you're going to ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required