SQL Server/T-SQL/View/Updatable View
Creating An INSTEAD OF DML Trigger
<source lang="sql">
4> 5> CREATE TABLE employee( 6> id INTEGER NOT NULL PRIMARY KEY, 7> first_name VARCHAR(10), 8> last_name VARCHAR(10), 9> salary DECIMAL(10,2), 10> start_Date DATETIME, 11> region VARCHAR(10), 12> city VARCHAR(20), 13> managerid INTEGER 14> ); 15> GO 1> INSERT INTO employee VALUES (1, "Jason" , "Martin", 5890,"2005-03-22","North","Vancouver",3); 2> GO 1> INSERT INTO employee VALUES (2, "Alison", "Mathews",4789,"2003-07-21","South","Utown",4); 2> GO 1> INSERT INTO employee VALUES (3, "James" , "Smith", 6678,"2001-12-01","North","Paris",5); 2> GO 1> INSERT INTO employee VALUES (4, "Celia" , "Rice", 5567,"2006-03-03","South","London",6); 2> GO 1> INSERT INTO employee VALUES (5, "Robert", "Black", 4467,"2004-07-02","East","Newton",7); 2> GO 1> INSERT INTO employee VALUES (6, "Linda" , "Green" , 6456,"2002-05-19","East","Calgary",8); 2> GO 1> INSERT INTO employee VALUES (7, "David" , "Larry", 5345,"2008-03-18","West","New York",9); 2> GO 1> INSERT INTO employee VALUES (8, "James" , "Cat", 4234,"2007-07-17","West","Regina",9); 2> GO 1> INSERT INTO employee VALUES (9, "Joan" , "Act", 6123,"2001-04-16","North","Toronto",10); 2> GO 1> 2> select * from employee; 3> GO id first_name last_name salary start_Date region city managerid
---------- ---------- ------------ ----------------------- ---------- -------------------- -----------
1 Jason Martin 5890.00 2005-03-22 00:00:00.000 North Vancouver 3 2 Alison Mathews 4789.00 2003-07-21 00:00:00.000 South Utown 4 3 James Smith 6678.00 2001-12-01 00:00:00.000 North Paris 5 4 Celia Rice 5567.00 2006-03-03 00:00:00.000 South London 6 5 Robert Black 4467.00 2004-07-02 00:00:00.000 East Newton 7 6 Linda Green 6456.00 2002-05-19 00:00:00.000 East Calgary 8 7 David Larry 5345.00 2008-03-18 00:00:00.000 West New York 9 8 James Cat 4234.00 2007-07-17 00:00:00.000 West Regina 9 9 Joan Act 6123.00 2001-04-16 00:00:00.000 North Toronto 10
1> 2> 3> 4> CREATE VIEW vw_employee 5> AS 6> SELECT ID, first_Name 7> FROM employee 8> GO 1> 2> CREATE TRIGGER trg_vw_employee 3> ON vw_employee 4> INSTEAD OF 5> INSERT 6> AS 7> SET NOCOUNT ON 8> INSERT employee(ID, first_Name) 9> SELECT i.ID, i.first_Name 10> FROM inserted i 11> GO 1> 2> INSERT vw_employee(ID, first_Name)VALUES (10,"P") 3> 4> select * from employee 5> GO id first_name last_name salary start_Date region city managerid
---------- ---------- ------------ ----------------------- ---------- -------------------- -----------
1 Jason Martin 5890.00 2005-03-22 00:00:00.000 North Vancouver 3 2 Alison Mathews 4789.00 2003-07-21 00:00:00.000 South Utown 4 3 James Smith 6678.00 2001-12-01 00:00:00.000 North Paris 5 4 Celia Rice 5567.00 2006-03-03 00:00:00.000 South London 6 5 Robert Black 4467.00 2004-07-02 00:00:00.000 East Newton 7 6 Linda Green 6456.00 2002-05-19 00:00:00.000 East Calgary 8 7 David Larry 5345.00 2008-03-18 00:00:00.000 West New York 9 8 James Cat 4234.00 2007-07-17 00:00:00.000 West Regina 9 9 Joan Act 6123.00 2001-04-16 00:00:00.000 North Toronto 10 10 P NULL NULL NULL NULL NULL NULL
1> 2> drop trigger trg_vw_employee 3> GO 1> drop view vw_employee 2> GO 1> 2> drop table employee; 3> GO 1>
</source>
Data modification via views
<source lang="sql">
1> create table employee( 2> ID int, 3> name nvarchar (10), 4> salary int, 5> start_date datetime, 6> city nvarchar (10), 7> region char (1)) 8> GO 1> 2> insert into employee (ID, name, salary, start_date, city, region) 3> values (1, "Jason", 40420, "02/01/94", "New York", "W") 4> GO 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (2, "Robert",14420, "01/02/95", "Vancouver","N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (3, "Celia", 24020, "12/03/96", "Toronto", "W") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (4, "Linda", 40620, "11/04/97", "New York", "N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (5, "David", 80026, "10/05/98", "Vancouver","W") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (6, "James", 70060, "09/06/99", "Toronto", "N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (7, "Alison",90620, "08/07/00", "New York", "W") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (8, "Chris", 26020, "07/08/01", "Vancouver","N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (9, "Mary", 60020, "06/09/02", "Toronto", "W") 3> GO (1 rows affected) 1> 2> select * from employee 3> GO ID name salary start_date city region
---------- ----------- ----------------------- ---------- ------
1 Jason 40420 1994-02-01 00:00:00.000 New York W 2 Robert 14420 1995-01-02 00:00:00.000 Vancouver N 3 Celia 24020 1996-12-03 00:00:00.000 Toronto W 4 Linda 40620 1997-11-04 00:00:00.000 New York N 5 David 80026 1998-10-05 00:00:00.000 Vancouver W 6 James 70060 1999-09-06 00:00:00.000 Toronto N 7 Alison 90620 2000-08-07 00:00:00.000 New York W 8 Chris 26020 2001-07-08 00:00:00.000 Vancouver N 9 Mary 60020 2002-06-09 00:00:00.000 Toronto W
(9 rows affected) 1> 2> -- Data modification via views 3> 4> CREATE VIEW myView 5> AS 6> SELECT * FROM Employee 7> GO 1> 2> 3> SELECT * FROM myView 4> GO ID Name Salary
---------- -----------
1 Jason 40420 2 Robert 14420 3 Celia 24020 4 Linda 40620 5 David 80026 6 James 70060 7 Alison 90620 8 Chris 26020 9 Mary 60020
(9 rows affected) 1> 2> INSERT myView (ID, Name) VALUES (99, "SQL") 3> GO (1 rows affected) 1> 2> SELECT * FROM myView 3> GO ID Name Salary
---------- -----------
1 Jason 40420 2 Robert 14420 3 Celia 24020 4 Linda 40620 5 David 80026 6 James 70060 7 Alison 90620 8 Chris 26020 9 Mary 60020 99 SQL NULL
(10 rows affected) 1> 2> SELECT * FROM Employee 3> GO ID name salary start_date city region
---------- ----------- ----------------------- ---------- ------
1 Jason 40420 1994-02-01 00:00:00.000 New York W 2 Robert 14420 1995-01-02 00:00:00.000 Vancouver N 3 Celia 24020 1996-12-03 00:00:00.000 Toronto W 4 Linda 40620 1997-11-04 00:00:00.000 New York N 5 David 80026 1998-10-05 00:00:00.000 Vancouver W 6 James 70060 1999-09-06 00:00:00.000 Toronto N 7 Alison 90620 2000-08-07 00:00:00.000 New York W 8 Chris 26020 2001-07-08 00:00:00.000 Vancouver N 9 Mary 60020 2002-06-09 00:00:00.000 Toronto W 99 SQL NULL NULL NULL NULL
(10 rows affected) 1> 2> 3> drop table employee 4> GO 1>
</source>