Jump to content

Creating a trigger to work on delete in SQL server

Go to solution Solved by Shammikit,

ok i managed to make one. if anyone else wants to know how its done you might find the example below useful

CREATE TRIGGER trgAfterDelete ON [Employee_Test] AFTER DELETE AS 
declare @empid int; 
declare @empname varchar(100); 
declare @empsal decimal(10,2); 
declare @audit_action varchar(100);
select @empid=d.Emp_ID from deleted d; 
select @empname=d.Emp_Name from deleted d; 
select @empsal=d.Emp_Sal from deleted d; 
set @audit_action='Deleted -- After Delete Trigger.';
insert into Employee_Test_Audit (Emp_ID, Emp_Name, Emp_Sal, Audit_Action, Audit_Timestamp) values (@empid, @empname, @empsal, @audit_action,getdate()); 
PRINT 'AFTER DELETE TRIGGER fired.

 

 

so i have a table where i want deleted records to be inserted to another table. how can i achieve this? i have already gone through the internet about this and i have found some codes as well, but i dont understand them. if some one could explain the two examples below it would be very helpful and i will try to make one for myself on my own.

------------------------------------------Example 1 ---------------------------------------------------------------
create trigger transaction_state after update on test_archive 
  begin 
    insert into test_me
        select * from test_archive where Transaction_status = 2; 
    delete from test_archive where Transaction_status = 2; 
  end;
--------------------------------------------------------------------------------------------------------------------

---------------------------------------Example 2 ------------------------------------------------------------------
ALTER TRIGGER [dbo].[HashTags_BeforeDelete]
    ON [dbo].[HashTags]
    FOR DELETE
AS
  BEGIN
    INSERT INTO HashTagsArchive(Id, HashTagId, delete_date)
       SELECT 
           d.Id, d.HashTagId, GETUTCDATE() 
       FROM deleted d 
  END
GO
---------------------------------------------------------------------------------------------------------------------

 

Link to post
Share on other sites

I've actually never used MySQL triggers but  I think:

 

For the first example with trigger name "transaction_state":

When you update a record on test_archive the trigger fires: you select entire rows (*: aka all column values) where the rows have the column Transaction_status equal to 2, and insert those rows into test_me. Then, you delete the rows you just selected (aka the ones with Transation_status=2) from test_archive.

 

 

Link to post
Share on other sites

ok i managed to make one. if anyone else wants to know how its done you might find the example below useful

CREATE TRIGGER trgAfterDelete ON [Employee_Test] AFTER DELETE AS 
declare @empid int; 
declare @empname varchar(100); 
declare @empsal decimal(10,2); 
declare @audit_action varchar(100);
select @empid=d.Emp_ID from deleted d; 
select @empname=d.Emp_Name from deleted d; 
select @empsal=d.Emp_Sal from deleted d; 
set @audit_action='Deleted -- After Delete Trigger.';
insert into Employee_Test_Audit (Emp_ID, Emp_Name, Emp_Sal, Audit_Action, Audit_Timestamp) values (@empid, @empname, @empsal, @audit_action,getdate()); 
PRINT 'AFTER DELETE TRIGGER fired.

 

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×