-- After Insert trigger
ALTER TRIGGER [Person].[ContactTypeTrigger]
ON [Person].[ContactType]
AFTER Insert, Update
AS
IF ('Batman' = (SELECT I.Name FROM inserted I))
BEGIN
RAISERROR('You can`t add Batman.',16,1);
ROLLBACK TRANSACTION;
END
IF Update(Name)
SELECT 'Name was modified from '
+ D.Name
+ ' to '
+ I.Name
FROM Inserted I
JOIN Deleted D
ON I.ContactTypeID = D.ContactTypeID;;
GO
-- Execution 1 (Success)
Update Person.ContactType
SET Name = 'Contractor' WHERE Name = 'Sales Manager'
-- [Output]
-- Name was modified from Sales Manager to Contractor
-- Execution 2 (Fails)
Update Person.ContactType
SET Name = 'Batman' WHERE Name = 'Contractor'
-- [Output]
-- Msg 50000,...
-- You can`t add Batman.