trigger syntax

instead of trigger

create trigger [owner.] trigger_name
	on [owner.]view_name
	instead of {insert, update, delete}
	as SQL_statements

instead of trigger with update clause

create trigger [owner.] trigger_name
	on [owner.]view_name
	instead of {insert, update, delete}
	as 
		[if update (column_name)
			[{and | or} update (column_name)]...]
			SQL_statements
		[if update (column_name)
			[{and | or} update (column_name)]...
			SQL_statements] ...

drop instead of trigger statement

The syntax for dropping the instead of trigger is the same as for the for triggers.

Parameters

Examples

Example 23

Example 1 These examples use instead of triggers to update union views.

CREATE TABLE EmployeeWest (
	empid                          int primary key,
	empname                    varchar(30),
	empdob                       datetime,
	region                           char(5)
		constraint region_chk
		   check (region='West'))
CREATE TABLE EmployeeEast (
	empid                          int primary key,
	empname                    varchar(30),
	empdob                       datetime,
	region                           char(5)
		constraint region_chk
		   check (region='East’))

CREATE VIEW Employees as
	SELECT * FROM EmployeeEast
	UNION ALL
	SELECT * FROM EmployeeWest

CREATE TRIGGER EmployeesInsertTrig on Employees
INSTEAD OF INSERT AS
BEGIN
	
	INSERT INTO EmployeeEast SELECT * FROM inserted where
			region = "East"

	INSERT INTO EmployeeWest SELECT * FROM inserted where
			region = "West"
END
--will insert the data into the EmployeeEast table
INSERT INTO Employees
		values (10, 'Jane Doe', '11/11/1967', 'East')
--will insert the data into the EmployeeWest table
INSERT INTO Employees
		values (11, 'John Smith', '01/12/1977', 'West')
--will insert multiple rows into EmployeeEast and 
--EmployeeWest tables. Employee2 table includes employees 
--from both East and West.
INSERT INTO Employees SELECT * from Employee2

Example 2 These examples use instead of triggers to implement encrypted column support,storing data in the database in encrypted form without changing applications..

CREATE TABLE Employee_t (id int PRIMARY KEY, name varchar(20), 
			salary binary (64))
--where the id and name columns are stored unencrypted, salary is
--encrypted and id is a primary key.
CREATE VIEW employee_v as select id, name, decrypt (salary)
FROM employee_t
CREATE TRIGGER EmployeeInsert
ON employee_v
INSTEAD OF INSERT
AS
BEGIN
	INSERT employee_t SELECT id, name, encrypt (salary)
	FROM inserted
END
CREATE TRIGGER employeeUpdate
ON employee_v
INSTEAD OF UPDATE
AS
BEGIN
	DELETE FROM employee_t WHERE id IN (SELECT id FROM
	deleted)
	INSERT employee_t SELECT id, name, encrypt (salary)
	FROM inserted
END
CREATE TRIGGER employeeDelete
ON employee_v
INSTEAD OF DELETE
AS
BEGIN
	DELETE FROM employee_t WHERE id IN (SELECT id FROM
	deleted)
END

Usage

Restrictions

Noteinstead of triggers on tables are not currently implemented.

Permissions

instead of triggers have the same permission requirements as for triggers: to create a view with instead of triggers, permission for insert/update/delete for the view, not the underlying tables, must be granted to the user.