A trigger is a special kind of stored procedure that executes when an INSERT, UPDATE, or DELETE statement modifies the data in a specified table. A trigger can query other tables and can include complex Transact-SQL statements. You often create triggers to enforce referential integrity or consistency among logically related data in different tables.
Example - Implementing an INSERT Trigger:
CREATE TRIGGER [insrtWorkOrder] ON [Production].[WorkOrder]
AFTER INSERT AS
BEGIN
SET NOCOUNT ON;
INSERT INTO [Production]. [TransactionHistory] (
[ProductID], [ReferenceOrderID], [TransactionType], [TransactionDate], [Quantity],[ActualCost] )
SELECT inserted.[ProductID], inserted.[WorkOrderID], 'W', GETDATE(), inserted.[OrderQty], 0
FROM inserted;
END;
Example 2 - Implementing a DELETE Trigger:
CREATE TRIGGER [delCategory] ON [Categories]
AFTER DELETE AS
BEGIN
UPDATE P SET [Discontinued] = 1
FROM [Products] P INNER JOIN deleted as d
ON P.[CategoryID] = d.[CategoryID]
END;
Hope this helps