Share via


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.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Customizing Development Settings in Visual Studio.

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