Trigger In SQL Server

Create Trigger

Syntax
CREATE TRIGGER TriggerName
ON TableName
FOR INSERT, UPDATE, DELETE
AS
[BEGIN]
	<Statements>
[END]
GO

Create Trigger

Example
CREATE TRIGGER TriggerAddDepartment
ON Department
FOR INSERT
AS
[BEGIN]
	SELECT * FROM Department;
[END]
GO

Trigger and Table inserted

Example
CREATE TRIGGER TriggerAddDepartment
ON Department
FOR INSERT
AS
[BEGIN]
	SELECT * FROM inserted;
[END]
GO

Trigger and Table inserted

Example
CREATE TRIGGER TriggerDeleteDepartment
ON Department
FOR DELETE
AS
[BEGIN]
	SELECT * FROM deleted;
[END]
GO

Trigger FOR vs INSTEAD OF

Example

CREATE TRIGGER TgAddDept
ON Department
FOR INSERT
AS
[BEGIN]
	SELECT * FROM inserted;
[END]
GO
CREATE TRIGGER TgAddDept
ON Department
INSTEAD OF INSERT
AS
[BEGIN]
	SELECT * FROM inserted;
[END]
GO