AFTER INSERT Triggers in SQL Server

The SQL Server AFTER INSERT Triggers will fire after the completion of the Insert operation. The SQL After Insert Triggers not Supported on Views. For this SQL Server After Insert trigger demo, we use the below-shown tables.

As you can see that our Employee table is Empty. Our task is to create SQL AFTER INSERT TRIGGER on this Employee table. And by using this SQL Server After Insert trigger, we want to Insert the records into Employee Table Audit, along with the audit information

Trigger Table 1

and our Employee Table Audit is also empty

Triggers Table 2

After INSERT Triggers in SQL Server Example

In this example, we will create an After Insert Triggers in SQL Server on the Employee table using the CREATE TRIGGER statement.

Remember, After Insert trigger will fire after the completion of Insert operation on the Employee table. Once it completes inserting into the Employee table, it will start inserting into the Employee audit table. If it fails to insert into the Employee table, then it won’t insert into the Audit table.

TIP: You can refer AFTER UPDATE TRIGGERS, TRIGGERS, and AFTER DELETE TRIGGERS articles in SQL Server.

-- Example for After INSERT Triggers in SQL Server

CREATE TRIGGER AfterINSERTTrigger on [EmployeeTable]
FOR INSERT 
AS DECLARE @EmpID INT,
	   @EmpName VARCHAR(50),
	   @EmpEducation VARCHAR(50),
	   @EmpOccupation VARCHAR(50),
	   @EmpYearlyIncome DECIMAL (10, 2), 
	   @EmpSales DECIMAL (10, 2); 

SELECT @EmpID = ins.ID FROM INSERTED ins;
SELECT @EmpName = ins.Name FROM INSERTED ins;
SELECT @EmpEducation = ins.Education FROM INSERTED ins;
SELECT @EmpOccupation = ins.Occupation FROM INSERTED ins;
SELECT @EmpYearlyIncome = ins.YearlyIncome FROM INSERTED ins;
SELECT @EmpSales = ins.Sales FROM INSERTED ins;

INSERT INTO [EmployeeTableAudit]( 
       [ID]
      ,[Name]
      ,[Education]
      ,[Occupation]
      ,[YearlyIncome]
      ,[Sales]
      ,[ServerName]
      ,[ServerInstanceName]
      ,[Insert Time])
VALUES (@EmpID,
	@EmpName,
	@EmpEducation,
	@EmpOccupation,
	@EmpYearlyIncome,
	@EmpSales,
	CAST( SERVERPROPERTY('MachineName') AS VARCHAR(50)), 
	CAST( SERVERPROPERTY('ServerName') AS VARCHAR(50)), 
	GETDATE()
	);
PRINT 'We Successfully Fired the AFTER INSERT Triggers in SQL Server.'
GO
After INSERT Triggers in SQL Server 3

First, we used the DECLARE Statement to declare the required variables

DECLARE @EmpID INT,
	@EmpName VARCHAR(50),
	@EmpEducation VARCHAR(50),
	@EmpOccupation VARCHAR(50),
	@EmpYearlyIncome DECIMAL (10, 2), 
	@EmpSales DECIMAL (10, 2);

Next, we used the SELECT Statement to select the records. The following statements will select one record from inserted values.

SELECT @EmpID = ins.ID FROM INSERTED ins;
SELECT @EmpName = ins.Name FROM INSERTED ins;
SELECT @EmpEducation = ins.Education FROM INSERTED ins;
SELECT @EmpOccupation = ins.Occupation FROM INSERTED ins;
SELECT @EmpYearlyIncome = ins.YearlyIncome FROM INSERTED ins;
SELECT @EmpSales = ins.Sales FROM INSERTED ins;

Next, we used the INSERT Statement to insert the selected values into the Employee Audit table. Here, the following statements returns the Machine Name and the server name. This information might be helpful for the audit

SERVERPROPERTY('MachineName'), 
SERVERPROPERTY('ServerName')

Let me show you the newly created SQL Server After Insert trigger. Go to the SQL Tutorial Database -> Go and expand the Employee Table -> and then expand the Triggers Folder

After INSERT Triggers in SQL Server 4

For the demonstration purpose, we are inserting five random records into the Employee table to check whether the After insert Trigger is triggered or not.

INSERT INTO [EmployeeTable] (
		[Name]
	       ,[Education]
	       ,[Occupation]
	       ,[YearlyIncome]
	       ,[Sales]
	     )
VALUES ('Tutorial Gateway', 'Masters', 'Admin', 150000, 900)
      ,('Imran Khan', 'Bachelors', 'Skilled Professional', 69000, 100)
      ,('Doe Lara', 'Bachelors', 'Management', 85000, 60)
      ,('Ramesh Kumar', 'High School', 'Professional', 45000, 630)
      ,('John Ruiz', 'Partial High School', 'Clerical', 40000, 220)
After INSERT Triggers in SQL Server 5

From the above screenshot, you can see that our Sql After Insert trigger is triggered, and also inserted one record into the Audit Table. Please use the following SQL Query to check the inserted records in the Employee table

SELECT [ID]
      ,[Name]
      ,[Education]
      ,[Occupation]
      ,[YearlyIncome]
      ,[Sales]
  FROM [EmployeeTable]
After INSERT Triggers in SQL Server 6

Next, check the records in the Employee table Audit using the following query.

SELECT [ID]
      ,[Name]
      ,[Education]
      ,[Occupation]
      ,[YearlyIncome]
      ,[Sales]
      ,[ServerName]
      ,[ServerInstanceName]
      ,[Insert Time]
  FROM [EmployeeTableAudit]
After INSERT Triggers in SQL Server 7

As you can see, our Audit Table is returning a single record. It is because, in our After Insert trigger definition, we are selecting only one record for each insert.

After INSERT Triggers in SQL Server Example 2

How to insert all the records into the audit table (triggered table) using the After Insert Triggers. For this, we will modify the trigger that we created in our previous example.

-- Second Example for After INSERT Triggers in SQL Server

CREATE TRIGGER AfterINSERTTriggerExample on [EmployeeTable]
FOR INSERT 
AS 
INSERT INTO [EmployeeTableAudit]( 
       [ID]
      ,[Name]
      ,[Education]
      ,[Occupation]
      ,[YearlyIncome]
      ,[Sales]
      ,[ServerName]
      ,[ServerInstanceName]
      ,[Insert Time])
SELECT  ID,
	    Name,
	    Education,
	    Occupation,
	    YearlyIncome,
	    Sales,
	    CAST( SERVERPROPERTY('MachineName') AS VARCHAR(50)), 
	    CAST( SERVERPROPERTY('ServerName') AS VARCHAR(50)), 
	    GETDATE()
FROM INSERTED;
PRINT 'We Successfully Fired Our Second AFTER INSERT Triggers in SQL Server.'
GO

Here, we are using the INSERT INTO SELECT Statement to select all the records that are inserting into the Employee table. Then we are inserting those records into the Audit table.

Next, let me insert some random records into the employee table

INSERT INTO [EmployeeTable] (
		    [Name]
		   ,[Education]
		   ,[Occupation]
		   ,[YearlyIncome]
		   ,[Sales]
	     )
VALUES ('Tutorial Gateway', 'Masters', 'Admin', 150000, 900)
      ,('Doe Lara', 'Bachelors', 'Management', 85000, 60)
      ,('Ramesh Kumar', 'High School', 'Professional', 45000, 630)
      ,('John Ruiz', 'Partial High School', 'Clerical', 40000, 220)
      ,('Imran Khan', 'Bachelors', 'Skilled Professional', 69000, 100)
After INSERT Triggers in SQL Server 8

As you see, our after insert trigger has fired, and also inserted all the records into the audit table. Let us see the Emp table

After INSERT Triggers in SQL Server 9

Next, check with the Employee Audit table.

After INSERT Triggers in SQL Server 10

From the above screenshot, the After Insert trigger has inserted all the records.

Categories SQL

Comments are closed.