Introduction
In this blog, I will explain SQL Triggers. Triggers are stored programs, which are automatically executed or fired when some events occur (insert, update, delete). Triggers are stored in and managed by the DBMS. They are used to maintain the referential integrity of data by changing the data in a systematic fashion. DBMS automatically fires the trigger as a result of a data modification to the associated table. Stored procedures are explicitly executed by invoking a call to the procedure while triggers are implicitly executed. In addition, triggers can also execute stored procedures. Triggers are used in the following events:
- DML-INSERT, UPDATE, DELETE
- DDL-CREATE, ALTER, DROP
Types of DML Triggers
- Before Triggers
- After Triggers
- Instead of Triggers
After Triggers
After triggers are invoked after DML (insert, update and delete) operations.
- After Insert
- After Update
- After Delete
- CREATE TRIGGER TRIGGER_NAME on TB_NAME
- FOR INSERT
- AS
- declare @COLUMN1 int, @COLUMN2 varchar(55), @audit_action varchar(100);
- select @COLUMN1 =i.TB_Columnname1 from inserted i;
- select @COLUMN2 =i.TB_Columnname2 from inserted i;
- set @audit_action='Inserted Record -- After Insert Trigger.';
- insert into TB_NAME(TB_Columnname1, TB_Columnname2,Audit_Action)
- values(@COLUMN1@COLUMN2);
- PRINT 'AFTER INSERT trigger fired.'

Onkar SharmaPosted Feb 2, 2020, 7:15 AM
Nice...very well explained...
Sourav Kumar DasPosted Nov 20, 2019, 10:26 PM
Nice good article for basics on Triggers.