Introduction
In this article I explained how to delete records in one table and after using an "after delete" trigger on the table and deleting records they are saved in another table. In a previous articles you can learn about the After Insert Trigger and After Update Trigger.
After Delete Trigger
A delete trigger is a kind of Stored Procedure that executes itself when a delete statement deletes data from a table on which the trigger is configured. When a delete trigger is fired the deleted rows from the affected table are placed in a logical deleted table. The logical deleted table is kind of a table that contains the copy of deleted rows from the affected table.
Features of After Delete Trigger
- The deleted table is always in the cache.
- Space is allocated from the memory to create the deleted table.
- A trigger that is defined for the delete action will not executes itself if the truncate statement is used because the truncated table is not logged and the delete trigger is not fired.
Why to use Triggers
- Provide auditing
- Prevent invalid transactions
- Maintain Synchronous table replicates
- Modify table data when DML statements are issued against views
- Automatically generate derived column values
After Triggers
An "after trigger" fired by DML statements can be defined only on tables, not on views. "After triggers" are executed after an insert, update or delete on a specified table.
- After Insert Trigger
- After Update Trigger
- After Delete Trigger
Enable or Disable the Trigger
You can enable or disable the trigger in database syntax:
alter table table_name enable trigger trigger_name
alter table table_name disable trigger trigger_name
For "After Delete" Trigger
Create Table-1
Create Database DemoTriggers
use DemoTriggers
Create table FirstTable
(
CustID int,
CustName varchar(max),
CustAddress nvarchar(max),
PaidAmmount decimal
)
Insert values into table fields.
insert into FirstTable values(101,'pankaj','vinod nagar',1200)
insert into FirstTable values(102,'Nimit Joshi','New Delhi',6000)
insert into FirstTable values(103,'Amit','West Vinod Nagar',800)

Create Table-2
Create table SecondTable
(
CustID int,
CustName varchar(max),
CustAddress nvarchar(max),
PaidAmmount decimal
)
There are no records in the Second Table until the "after delete" trigger is fired.
Create Procedure for Delete
Create proc [dbo].[DeleteData]
@cid int
as
begin
delete from FirstTable where CustID=@cid
end
Create Trigger For Delete
Create trigger [dbo].[trgDelete] on [dbo].[FirstTable]
for delete
as
declare @cid int;
declare @cname varchar(max);
declare @cadd nvarchar(max);
declare @pammount decimal;
select @cid=i.CustID from deleted i;
select @cname=i.CustName from deleted i;
select @cadd=i.CustAddress from deleted i;
select @pammount=i.PaidAmmount from deleted i;
insert into SecondTable values(@cid,@cname,@cadd,@pammount)
The create Trigger statement is used to create a trigger and an "on" clause specifies the table name on which the trigger is to be attached. In this trigger the deleted records are accessed from the logical table, the "logical deleted table" and inserted into the second table.












Comments
Join the conversation! Your thoughts help the community grow.