| title | Create DML Triggers | Microsoft Docs | ||||||||
|---|---|---|---|---|---|---|---|---|---|
| ms.custom | |||||||||
| ms.date | 09/01/2017 | ||||||||
| ms.prod | sql | ||||||||
| ms.reviewer | |||||||||
| ms.technology | |||||||||
| ms.topic | conceptual | ||||||||
| helpviewer_keywords |
|
||||||||
| ms.assetid | b2b52258-642b-462e-8e0f-18c09d2eccf4 | ||||||||
| author | rothja | ||||||||
| ms.author | jroth | ||||||||
| monikerRange | =azuresqldb-current||>=sql-server-2016||=sqlallproducts-allversions||>=sql-server-linux-2017||=azuresqldb-mi-current |
[!INCLUDEtsql-appliesto-ss2008-asdb-xxxx-xxx-md] This topic describes how to create a [!INCLUDEtsql] DML trigger by using [!INCLUDEssManStudioFull] and by using the [!INCLUDEtsql] CREATE TRIGGER statement.
For a list of limitations and restrictions related to creating DML triggers, see CREATE TRIGGER (Transact-SQL).
Requires ALTER permission on the table or view on which the trigger is being created.
You can use one of the following:
-
In Object Explorer, connect to an instance of [!INCLUDEssDE] and then expand that instance.
-
Expand Databases, expand the [!INCLUDEssSampleDBobject] database, expand Tables and then expand the table Purchasing.PurchaseOrderHeader.
-
Right-click Triggers, and then select New Trigger.
-
On the Query menu, click Specify Values for Template Parameters. Alternatively, you can press (Ctrl-Shift-M) to open the Specify Values for Template Parameters dialog box.
-
In the Specify Values for Template Parameters dialog box, enter the following values for the parameters shown.
Parameter Value Author Your name Create Date Today's date Description Checks the vendor credit rating before allowing a new purchase order with the vendor to be inserted. Schema_Name Purchasing Trigger_Name NewPODetail2 Table_Name PurchaseOrderDetail Data_Modification_Statement Remove UPDATE and DELETE from the list. -
Click OK.
-
In the Query Editor, replace the comment
-- Insert statements for trigger herewith the following statement:IF @@ROWCOUNT = 1 BEGIN UPDATE Purchasing.PurchaseOrderHeader SET SubTotal = SubTotal + LineTotal FROM inserted WHERE PurchaseOrderHeader.PurchaseOrderID = inserted.PurchaseOrderID END ELSE BEGIN UPDATE Purchasing.PurchaseOrderHeader SET SubTotal = SubTotal + (SELECT SUM(LineTotal) FROM inserted WHERE PurchaseOrderHeader.PurchaseOrderID = inserted.PurchaseOrderID) WHERE PurchaseOrderHeader.PurchaseOrderID IN (SELECT PurchaseOrderID FROM inserted) END;
-
To verify the syntax is valid, on the Query menu, click Parse. If an error message is returned, compare the statement with the information above and correct as needed and repeat this step.
-
To create the DML trigger, from the Query menu, click Execute. The DML trigger is created as an object in the database.
-
To see the DML trigger listed in Object Explorer, right-click Triggers and select Refresh.
-
In Object Explorer, connect to an instance of [!INCLUDEssDE] and then expand that instance.
-
From the File menu, click New Query.
-
Copy and paste the following example into the query window and click Execute. This example creates the same stored DML trigger as above.
-- Trigger valid for multirow and single row inserts -- and optimal for single row inserts. USE AdventureWorks2012; GO CREATE TRIGGER NewPODetail3 ON Purchasing.PurchaseOrderDetail FOR INSERT AS IF @@ROWCOUNT = 1 BEGIN UPDATE Purchasing.PurchaseOrderHeader SET SubTotal = SubTotal + LineTotal FROM inserted WHERE PurchaseOrderHeader.PurchaseOrderID = inserted.PurchaseOrderID END ELSE BEGIN UPDATE Purchasing.PurchaseOrderHeader SET SubTotal = SubTotal + (SELECT SUM(LineTotal) FROM inserted WHERE PurchaseOrderHeader.PurchaseOrderID = inserted.PurchaseOrderID) WHERE PurchaseOrderHeader.PurchaseOrderID IN (SELECT PurchaseOrderID FROM inserted) END;