Export (0) Print
Expand All

How to: Create a Trigger 

You can write triggers in Transact-SQL for Microsoft® SQL Server™ databases or PL/SQL for Oracle databases. You create a trigger by specifying:

  • The current table or view (in the case of INSTEAD OF triggers).

  • The data modification transactions that activate the trigger; adding new data (INSERT), updating existing data (UPDATE), or deleting existing data (DELETE).

  • The actions that the trigger will take immediately following the transactions you specify.

NoteNote

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

To create a trigger

  1. In Server Explorer, expand the Tables folder.

  2. Right-click the name of the table or view that you want to create a trigger on.

  3. Choose Add New Trigger on the shortcut menu.

    A new trigger is created in the source code editor with skeleton SQL statements:

    CREATE TRIGGER authors_Trigger1
    ON dbo.authors
    FOR /* INSERT, UPDATE, DELETE */
    AS
       /* IF UPDATE (column_name) . . .*/
    

    -or-

    CREATE TRIGGER titleview_Trigger1
    ON dbo.titleview
    INSTEAD OF /* INSERT, UPDATE, DELETE */
    AS
       /* IF UPDATE (column_name) . . .*/
    
  4. Modify the default trigger text as follows:

    Replace With

    authors_Trigger1

    The name you want to assign to the trigger

    /*INSERT, UPDATE, DELETE */

    The type of actions that will activate this trigger

    /*IF UPDATE (column_name) . . .*/

    The transactions that will take place

  5. Write the remaining trigger text in SQL.

See Also

Other Resources

Working with Triggers

Community Additions

ADD
Show:
© 2014 Microsoft