locked
On INSERT call Trigger RRS feed

  • Question

  • User-507786106 posted
    I have a table and need to create a Trigger for each time a record is Inserted into my ABCDoc table.

    Goal: On INSERT create a Trigger, how to make that happen?
    Wednesday, July 12, 2017 9:01 PM

Answers

  • User-271186128 posted

    Hi slimbunny,

    I have a table and need to create a Trigger for each time a record is Inserted into my ABCDoc table.

    Goal: On INSERT create a Trigger, how to make that happen?

    Do you mean you want to create a Trigger? If that is the case, I suggest you could refer to the following code:

    CREATE TRIGGER trgAfterInsert ON [dbo].[Employee_Test] 
    FOR INSERT
    AS
    	declare @empid int;
    	declare @empname varchar(100);
    	declare @empsal decimal(10,2);
    	declare @audit_action varchar(100);
    
    	select @empid=i.Emp_ID from inserted i;	
    	select @empname=i.Emp_Name from inserted i;	
    	select @empsal=i.Emp_Sal from inserted i;	
    	set @audit_action='Inserted Record -- After Insert 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 INSERT trigger fired.'
    GO

    More details about trigger, see:

    https://www.codeproject.com/Articles/25600/Triggers-SQL-Server

    https://docs.microsoft.com/en-us/sql/t-sql/statements/create-trigger-transact-sql

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 13, 2017 8:37 AM