Trigger trgCustomer_Deletion

Avoid deleting record that is linked to another table in this sample the tables involved are 
[tblCustomers] and [tblOrders] linked to column [customerId]
CREATE TRIGGER trgCustomer_Deletion
ON tblCustomers
INSTEAD OF DELETE
AS
BEGIN

    DELETE c
    FROM  tblcustomers c
    JOIN  deleted d
    ON  c.customerId = d.customerId
    WHERE NOT EXISTS (SELECT 1
                       FROM tblOrders
                       WHERE customerId = d.customerId);
END 
 

Add Feedback