CREATE TRIGGER FOR INSERT, UPDATE : Trigger for update « Trigger « SQL Server / T-SQL Tutorial

Home
SQL Server / T-SQL Tutorial
1.Query
2.Insert Delete Update
3.Table
4.Table Join
5.Data Types
6.Set Operations
7.Constraints
8.Subquery
9.Aggregate Functions
10.Date Functions
11.Math Functions
12.String Functions
13.Data Convert Functions
14.Analytical Functions
15.Sequence Indentity
16.View
17.Index
18.Cursor
19.Database
20.Transact SQL
21.Procedure Function
22.Trigger
23.Transaction
24.XML
25.System Functions
26.System Settings
27.System Tables Views
28.User Role
29.CLR
SQL Server / T-SQL Tutorial » Trigger » Trigger for update 
22.6.4.CREATE TRIGGER FOR INSERT, UPDATE
6>
7CREATE TABLE Customers (
8>      CustomerID nchar (5NOT NULL ,
9>      CompanyName nvarchar (40NOT NULL ,
10>     ContactName nvarchar (30NULL ,
11>     ContactTitle nvarchar (30NULL ,
12>     Address nvarchar (60NULL ,
13>     City nvarchar (15NULL ,
14>     Region nvarchar (15NULL ,
15>     PostalCode nvarchar (10NULL ,
16>     Country nvarchar (15NULL ,
17>     Phone nvarchar (24NULL ,
18>     Fax nvarchar (24NULL
19)
20> GO
1>
2INSERT Customers VALUES('1','A','Maria',    'Sales',  'Str. 57', 'Berlin'    ,NULL,'12209', 'Germany','111-1111111','111-1111111')
3INSERT Customers VALUES('2','M','Joe',      'Owner',  'Ave. 231','Vancouver' ,NULL,'05023', 'Mexico', '(222222-3332',NULL)
4INSERT Customers VALUES('3','H','Thomas',   'Sales',  'Sq.  111','London'    ,NULL,'1D00P', 'UK',     '(444444-4444','(444444-4444')
5INSERT Customers VALUES('4','B','Berg',     'Order',  'Blv    8','Toronto'   ,NULL,'00222', 'Sweden', '4444-55 55 65','5555-55 55 55')
6INSERT Customers VALUES('5','S','Moos',     'Sales',  'Fort  57','New York'  ,NULL,'68306', 'Germany','6666-66666','6666-77777')
7INSERT Customers VALUES('6','F','Cite',     'Manager','24      ','Dalles'    ,NULL,'67000', 'France', '88.60.15.31','88.60.15.32')
8INSERT Customers VALUES('7','C','Sommer',   'Owner',  'Araq, 67','Paris'     ,NULL,'28023', 'Spain',  '(91555 22 82','(91555 91 99')
9INSERT Customers VALUES('8','P','Leb',      'Owner',  '12      ','Beijing'   ,NULL,'13008', 'France', '91.24.45.40','91.24.45.41')
10INSERT Customers VALUES('9','D','Elizabeth','Manager','23 Blvd.','Tsawassen','BC', 'T2F8M4','Canada', '(604555-4729','(604555-3745')
11> go

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)
1>
2CREATE TABLE Orders (
3>      OrderID int IDENTITY (11NOT NULL ,
4>      CustomerID nchar (5NULL ,
5>      EmployeeID int NULL ,
6>      OrderDate datetime NULL ,
7>      RequiredDate datetime NULL ,
8>      ShippedDate datetime NULL ,
9>      ShipVia int NULL ,
10>     Freight money NULL DEFAULT (0),
11>     ShipName nvarchar (40NULL ,
12>     ShipAddress nvarchar (60NULL ,
13>     ShipCity nvarchar (15NULL ,
14>     ShipRegion nvarchar (15NULL ,
15>     ShipPostalCode nvarchar (10NULL ,
16>     ShipCountry nvarchar (15NULL)
17> GO
1>
2>    CREATE TRIGGER OrderHasCustomer
3>       ON Orders
4>       FOR INSERT, UPDATE
5>    AS
6>       IF EXISTS
7>          (
8>           SELECT 'True'
9>           FROM Inserted i
10>           LEFT JOIN Customers c
11>              ON i.CustomerID = c.CustomerID
12>           WHERE c.CustomerID IS NULL
13>          )
14>       BEGIN
15>          RAISERROR('Order Must Have Valid CustomerID',16,1)
16>          ROLLBACK TRAN
17>       END
18> GO
1>
2> drop TRIGGER OrderHasCustomer;
3> GO
1>
2> drop table Customers;
3> GO
1> drop table orders;
2> GO
22.6.Trigger for update
22.6.1.create TRIGGER for update
22.6.2.The trigger rolls back any T-SQL that changes the advance column.
22.6.3.Cascade Update Triggers
22.6.4.CREATE TRIGGER FOR INSERT, UPDATE
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.