Sei sulla pagina 1di 4

Using Triggers In MS SQL Server - What

exactly is a trigger?
(Page 2 of 4 )

A trigger is an object contained within an SQL Server database that is used to execute a
batch of SQL code whenever a specific event occurs. As the name suggests, a trigger is
“fired” whenever an INSERT, UPDATE, or DELETE SQL command is executed against
a specific table.

Triggers are associated with a single table, and are automatically executed internally by
SQL Server. Let’s create a very basic trigger now (I am using Microsoft SQL Server 7.0
on a Windows 2000 machine).

Start by opening Enterprise Manager (Start -> Programs -> Microsoft SQL Server 7.0 ->
Enterprise Manager). In this example we will create our trigger against the “authors”
table of the "pubs" database, so drill down through the tree view in the left pane until you
can see the "“authors" table of the "pubs" database in the right pane, like this:

Delete all the text in the text box; we won’t need it because we’re creating our trigger
from absolute scratch. All triggers are created using the "CREATE TRIGGER"
command. The syntax of the “"REATE TRIGGER" command is shown below:

CREATE TRIGGER trigger_name

ON { table | view }

[ WITH ENCRYPTION ]

{ { FOR | AFTER | INSTEAD OF } { [ INSERT ] [ , ] [ UPDATE ] [ , ] [ DELETE ] }

[ 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 ]

It looks really confusing, but it’s actually quite simple. I won’t go into detail about it, but
you can visit http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/tsqlref/ts_create2_7eeq.asp for the full explanation. Enter the following SQL code into
the text box:

CREATE TRIGGER trig_addAuthor

ON authors

FOR INSERT

AS

-- Get the first and last name of new author

DECLARE @newName VARCHAR(100)

SELECT @newName = (SELECT au_fName + ' ' + au_lName FROM Inserted)

-- Print the name of the new author

PRINT 'New author "' + @newName + '" added.'

Click on the "OK" button. We have just created a new trigger named "trig_addAuthor",
which is attached to the "authors" table of the "pubs" database. Whenever a new record is
added to the "authors" table, SQL Server will automatically execute our trigger. Let’s
discuss the actual SQL code that makes up the trigger:

CREATE TRIGGER trig_addAuthor

ON authors

These two lines tell SQL server that we want to create a new trigger object named
"trig_addAuthor", which will be attached to the "authors" table.

FOR INSERT

Here, we have specified that our trigger will be executed whenever an "INSERT"
command is executed against the "authors" table. Other possible options include
"UPDATE" and "DELETE", which would be triggered when one/more rows in the
"authors" table were either updated or deleted.

If we wanted, we could handle more than one type of query in one trigger. For example,
to handle both "INSERT" and "UPDATE", we would use "FOR INSERT, UPDATE".

AS

DECLARE @newName VARCHAR(100)

SELECT @newName = (SELECT au_fName + ' ' + au_lName FROM Inserted)

Any code after the "AS" keyword is actually executed when the trigger is called. It’s
important to note that this part of the trigger can contain any code that a standard stored
procedure could contain. You can also call stored procedures using the "EXEC"
command from within the body of the trigger.

We have created a new variable named "newName". "newName" is a variable length


character value that can hold a maximum of one hundred characters. On the next line, we
assign the value of an SQL query to the "newName" variable.

SELECT au_fName + ' ' + au_lName FROM Inserted

We can see that this SQL command retrieves the au_fName and au_lName fields from
the "Inserted" table. The "Inserted" table is a virtual table which contains all of the fields
and values from the actual "INSERT" command that made SQL Server call the trigger in
the first place.

To understand what I mean, let's take a look at the design of the actual "authors" table in
the "pubs" database. Right click on it and choose Design Table:
T
h
Next: UPDATE and DELETE triggers >>

Potrebbero piacerti anche