Professional Documents
Culture Documents
Trigger Sample
Trigger Sample
Trigger Sample
(
SSN char(11) PRIMARY KEY,
Name nvarchar(100),
Address nvarchar(100),
Birthdate datetime
)
This view reports all relevant data from the two tables for a person:
You can record attempts to insert rows with duplicate social security numbers. The
PersonDuplicates table logs the inserted values, the name of the user who
attempted the insert, and the time of the insert:
The INSTEAD OF trigger inserts rows into multiple base tables from a single view.
Attempts to insert rows with duplicate social security numbers are recorded in the
PersonDuplicates table. Duplicate rows in the EmployeeTable are changed to update
statements.
==================================================================================
Examples
A. Use an AFTER INSERT trigger
The following example assumes the existence of a table called newsale in the pubs
database. This the CREATE statement for newsale:
If you want to examine each of the records you are trying to insert, the trigger
conditionalinsert analyzes the insert row by row, and then deletes the rows that
do not have a title_id in titles.
To test the trigger, insert four rows in the newsale table. Two of the newsale
rows
have title_ids that do not match any of those already in the titles table:
=================================================================================