A trigger is a special kind of a store procedure that executes in response to certain action on the table like insertion, deletion or upadte of data. It is a database object which is bound to a table and is executed automatically.
You should have the following privileges:
CREATE TRIGGER dEmployee ON HumanResources.Employee
INSTEAD OF DELETE NOT FOR REPLICATION AS
BEGIN
DECLARE @Count int;
SET @Count = @@ROWCOUNT;
IF @Count = 0
RETURN;
SET NOCOUNT ON;
BEGIN
RAISERROR
(N'Employees cannot be deleted. They can only be marked as not current.', -- Message
10, -- Severity.
1); -- State.
-- Rollback any active or uncommittable transactions
IF @@TRANCOUNT > 0
BEGIN
ROLLBACK TRANSACTION;
END
END;
END;
To simplify the process of a Trigger creation, you may use the CreateTrigger snippet.
The following code will be inserted to the document.
Edit a view by selecting Edit Trigger from the Database Explorer node shortcut menu. In the opened document, you can alter the query text. Save the document to apply changes you’ve made to the query text.
To drop a trigger, click Delete on the Database Explorer node shortcut menu.
To enable or disable a trigger, click Enable or Disable on the Database Explorer node shortcut menu.
To enable or disable all triggers at once, select Enable All or Disable ALL from the Database Explorer node shortcut menu.