Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Triggers

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

Triggers

SQL Server provides three type of triggers:


• Data manipulation language (DML) triggers which are invoked
automatically in response to INSERT, UPDATE, and DELETE events
against tables.
• Data definition language (DDL) triggers which fire in response to
CREATE, ALTER, and DROP statements. DDL triggers also fire in
response to some system stored procedures that perform DDL-like
operations.
• Logon triggers which fire in response to LOGON events
SQL Server CREATE TRIGGER

• The CREATE TRIGGER statement allows you to create a new trigger


that is fired automatically whenever an event such as INSERT, DELETE,
or UPDATE occurs against a table.
• The following illustrates the syntax of the CREATE TRIGGER statement:
• In this syntax:
• The schema_name is the name of the schema to which the new
trigger belongs. The schema name is optional.
• The trigger_name is the user-defined name for the new trigger.
• The table_name is the table to which the trigger applies.
• The event is listed in the AFTER clause. The event could be INSERT,
UPDATE, or DELETE. A single trigger can fire in response to one or
more actions against the table.
• The NOT FOR REPLICATION option instructs SQL Server not to fire the
trigger when data modification is made as part of a replication
process.
• The SQL_statements is one or more Transact-SQL used to carry out
actions once an event occurs.
“Virtual” tables for triggers: INSERTED and DELETED

• SQL Server provides two virtual tables that are available specifically
for triggers called INSERTED and DELETED tables. SQL Server uses
these tables to capture the data of the modified row before and after
the event occurs.
SQL Server INSTEAD OF Trigger

• What is an INSTEAD OF trigger


• An INSTEAD OF trigger is a trigger that allows you to skip an INSERT,
DELETE, or UPDATE statement to a table or a view and execute other
statements defined in the trigger instead. The actual insert, delete, or
update operation does not occur at all.
• In other words, an INSTEAD OF trigger skips a DML statement and
execute other statements.
The following illustrates the syntax of how to
create an INSTEAD OF trigger:
References:
• https://www.sqlservertutorial.net/sql-server-triggers/
• https://www.slideserve.com/jneal/advanced-sql-powerpoint-ppt-presEntation

You might also like