SQL Server/T-SQL/Trigger/Create Trigger

Материал из SQL эксперт
Перейти к: навигация, поиск

Creating and Using Triggers

   <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 rows affected) 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> -- Creating and Using Triggers 3> 4> 5> CREATE TRIGGER myTrigger 6> ON employee 7> FOR UPDATE, INSERT, DELETE 8> AS 9> SELECT * FROM inserted 10> SELECT * FROM deleted 11> GO 1> 2> 3> SELECT "Before INSERT" 4> INSERT Employee (ID, Name) VALUES (31, "Rick") 5> GO


Before INSERT (1 rows affected) ID name salary start_date city region


---------- ----------- ----------------------- ---------- ------
        31 Rick              NULL                    NULL NULL       NULL

(1 rows affected) ID name salary start_date city region


---------- ----------- ----------------------- ---------- ------

(0 rows affected) 1> 2> SELECT "Before UPDATE" 3> UPDATE Employee 4> SET Name = "Rickie" 5> WHERE ID = 3 6> GO


Before UPDATE (1 rows affected) ID name salary start_date city region


---------- ----------- ----------------------- ---------- ------
         3 Rickie           24020 1996-12-03 00:00:00.000 Toronto    W

(1 rows affected) ID name salary start_date city region


---------- ----------- ----------------------- ---------- ------
         3 Celia            24020 1996-12-03 00:00:00.000 Toronto    W

(1 rows affected) 1> 2> SELECT "Before DELETE" 3> DELETE FROM Employee WHERE ID = 3 4> GO


Before DELETE (1 rows affected) ID name salary start_date city region


---------- ----------- ----------------------- ---------- ------

(0 rows affected) ID name salary start_date city region


---------- ----------- ----------------------- ---------- ------
         3 Rickie           24020 1996-12-03 00:00:00.000 Toronto    W

(1 rows affected) 1> 2> 3> drop table employee 4> GO 1>

      </source>
   
  


Creating a Trigger and raise an error

   <source lang="sql">

1> create table employee( 2> ID int, 3> name nvarchar (10), 4> salary int ) 5> GO 1> 2> create table job( 3> ID int, 4> title nvarchar (10), 5> averageSalary int) 6> GO 1> 2> 3> insert into employee (ID, name, salary) values (1, "Jason", 1234) 4> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (2, "Robert", 4321) 2> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (3, "Celia", 5432) 2> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (4, "Linda", 3456) 2> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (5, "David", 7654) 2> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (6, "James", 4567) 2> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (7, "Alison", 8744) 2> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (8, "Chris", 9875) 2> GO (1 rows affected) 1> insert into employee (ID, name, salary) values (9, "Mary", 2345) 2> GO (1 rows affected) 1> 2> insert into job(ID, title, averageSalary) values(1,"Developer",3000) 3> GO (1 rows affected) 1> insert into job(ID, title, averageSalary) values(2,"Tester", 4000) 2> GO (1 rows affected) 1> insert into job(ID, title, averageSalary) values(3,"Designer", 5000) 2> GO (1 rows affected) 1> insert into job(ID, title, averageSalary) values(4,"Programmer", 6000) 2> GO (1 rows affected) 1> 2> 3> select * from employee; 4> GO ID name salary


---------- -----------
         1 Jason             1234
         2 Robert            4321
         3 Celia             5432
         4 Linda             3456
         5 David             7654
         6 James             4567
         7 Alison            8744
         8 Chris             9875
         9 Mary              2345

(9 rows affected) 1> select * from job; 2> GO ID title averageSalary


---------- -------------
         1 Developer           3000
         2 Tester              4000
         3 Designer            5000
         4 Programmer          6000

(4 rows affected) 1> 2> 3> -- Creating a Trigger 4> 5> CREATE TRIGGER tr_DelData 6> ON Employee 7> FOR DELETE 8> AS 9> IF (SELECT Count(*) FROM job) > 0 10> BEGIN 11> RAISERROR 50009 "Cannot delete an employee" 12> ROLLBACK TRANSACTION 13> RETURN 14> END 15> GO 1> 2> delete employee 3> GO Msg 50009, Level 16, State 1, Server sqle\SQLEXPRESS, Procedure tr_DelData, Line 11 Cannot delete an employee Msg 3609, Level 16, State 1, Server sqle\SQLEXPRESS, Line 2 The transaction ended in the trigger. The batch has been aborted. 1> 2> select * from employee 3> GO ID name salary


---------- -----------
         1 Jason             1234
         2 Robert            4321
         3 Celia             5432
         4 Linda             3456
         5 David             7654
         6 James             4567
         7 Alison            8744
         8 Chris             9875
         9 Mary              2345

(9 rows affected) 1> 2> drop trigger tr_DelData 3> drop table employee; 4> drop table job; 5> GO 1>

      </source>
   
  


Rolling Back in an AFTER Trigger

   <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 rows affected) 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> --Rolling Back in an AFTER Trigger 3> 4> CREATE TRIGGER myTriggerRollback 5> ON Employee 6> FOR INSERT 7> AS 8> ROLLBACK TRAN 9> PRINT "INSERT statement rolled back." 10> GO 1> 2> DISABLE TRIGGER myTriggerRollback ON Employee 3> 4> SELECT * FROM Employee 5> 6> INSERT Employee (ID, Name) VALUES (31, "Rick") 7> 8> SELECT * FROM Employee 9> 10> DELETE FROM Employee WHERE ID = 31 11> 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

(1 rows affected) 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
        31 Rick              NULL                    NULL NULL       NULL

(1 rows affected) 1> 2> ENABLE TRIGGER myTriggerRollback ON Employee 3> 4> SELECT * FROM Employee 5> 6> INSERT Employee (ID, Name) VALUES (31, "Rick") 7> 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

INSERT statement rolled back. Msg 3609, Level 16, State 1, Server sqle\SQLEXPRESS, Line 6 The transaction ended in the trigger. The batch has been aborted. (9 rows affected) 1> SELECT * FROM Employee 2> 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> drop table employee 3> GO 1>

      </source>