INSTEAD OF INSERT on a view : Trigger on view « Trigger « SQL Server / T-SQL Tutorial






6> CREATE TABLE orders
7>         (orderid INT NOT NULL,
8>         price MONEY NOT NULL,
9>         quantity INT NOT NULL,
10>         orderdate DATETIME NOT NULL,
11>         total AS price * quantity,
12>         shippeddate AS DATEADD (DAY, 7, orderdate))
13> GO
1>
2> CREATE VIEW all_orders
3>         AS SELECT orderid, price, quantity, orderdate, total, shippeddate
4>         FROM orders
5> GO
1>
2> CREATE TRIGGER tr_orders ON all_orders INSTEAD OF INSERT
3> AS BEGIN
4>    INSERT INTO orders SELECT orderid, price, quantity, orderdate FROM inserted
5> END
6> GO
1>
2> drop trigger tr_orders;
3> GO
1>
2> drop view all_orders;
3> GO
1>
2> drop table orders;








22.9.Trigger on view
22.9.1.INSTEAD OF INSERT on a view
22.9.2.CREATE INSTEAD OF DELETE TRIGGER ON a view
22.9.3.INSTEAD OF UPDATE trigger on a view