INSTEAD of UPDATE Triggers in SQL Server

The SQL INSTEAD OF UPDATE trigger will fire before the execution starts. So, you can use this SQL Server INSTEAD OF UPDATE trigger to pass the value for Identity Columns or Updating different tables, etc.

For this SQL INSTEAD OF UPDATE trigger demonstration, we are going to use the tables that we have shown below. Here, our task is to create an SQL Server INSTEAD OF UPDATE TRIGGER on this Employee table. And by using this Instead of UPDATE Trigger, we want to restrict the records Update

Instead Of UPDATE Triggers in SQL Server 1

And our Employee Table Audit also holds the same 14 records, along with the Server name, Server Instance name, and Insert Time (Audit Information).

Instead Of UPDATE Triggers in SQL Server 2

Instead Of UPDATE Triggers in SQL Server Example

SQL Instead of Update Triggers can create on Tables, and Views. In general, we use these triggers on Views. In this example, we will show how to create an Instead of update Triggers in SQL Server. For instance, if you want to restrict the users from updating the records in the original table. And you want those updated logs in another historical table, use this trigger.

TIP: You can refer Views, TRIGGERS, INSTEAD OF INSERT TRIGGERS, and INSTEAD OF DELETE TRIGGERS articles in SQL Server.

Here, we will create an Instead of UPDATE Triggers in SQL Server on the Employee table using the CREATE TRIGGER statement. From the below code snippet, you can see that 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.

It means, when the user updates any Employee table record, then the trigger will insert those records into an audit table, and kept the Employee table unchanged.

-- Example for INSTEAD OF UPDATE Triggers in SQL Server

CREATE TRIGGER InsteadOfUPDATETriggerExample on [EmployeeTable]
INSTEAD OF UPDATE 
AS DECLARE @ActionPeformed VARCHAR(50)

IF UPDATE(YearlyIncome)
BEGIN
     SET @ActionPeformed = 'Updated Yearly Income'
END
ELSE BEGIN
      SET @ActionPeformed = 'Updated Sales'
END

INSERT INTO [EmployeeTableAudit]( 
       [ID]
      ,[Name]
      ,[Education]
      ,[Occupation]
      ,[YearlyIncome]
      ,[Sales]
      ,[Update Time]
     ,[ActionPerformed])
SELECT ID,
	Name,
	Education,
	Occupation,
	YearlyIncome,
	Sales,
	GETDATE(),
	@ActionPeformed
FROM INSERTED;
PRINT 'We Successfully Fired Our First INSTEAD OF UPDATE Triggers in SQL Server.'
GO
Instead Of UPDATE Triggers in SQL Server 3

Let me show you the newly created trigger by opening the Object Explorer -> Go to the Database -> Go and expand the Employee Table -> and then expand the Triggers Folder

Instead Of UPDATE Triggers in SQL Server 4

For the demo purpose, we are updating the Yearly Income as 123,456, and Sales = 5000 for all the records in the Employee table whose Occupation = Professional.

-- SQL INSTEAD OF UPDATE Triggers Example

UPDATE [EmployeeTable]
	SET [YearlyIncome] = 123456, [Sales] = 5000
	WHERE [Occupation] = N'Professional'
Instead Of UPDATE Triggers in SQL Server 5

From the above screenshot, you can see that our trigger is triggered. And, instead of inserting 4 records into the Employee Audit table. Please use the following Query to check the inserted records in the Employee table

-- SQL INSTEAD OF UPDATE Triggers Example

SELECT [ID]
      ,[Name]
      ,[Education]
      ,[Occupation]
      ,[YearlyIncome]
      ,[Sales]
  FROM [EmployeeTable]
Instead Of UPDATE Triggers in SQL Server 6

Though we updated the Employee table, As you can see from the above screenshot, our Employee table is unchanged. Next, check the records in the Employee table Audit using the following query.

-- SQL Server INSTEAD OF UPDATE Triggers Example

SELECT [Name]
      ,[Education]
      ,[Occupation]
      ,[YearlyIncome]
      ,[Sales]
      ,[ServerName]
      ,[ServerInstanceName]
      ,[Insert Time]
  FROM [EmployeeTableAudit]
Instead Of UPDATE Triggers in SQL Server 7

Here, you can see that the trigger has interested 4 new records into Employee audit table

Instead of UPDATE Triggers in SQL Server Example 2

Let us see how to Update all the records in the audit table (triggered table) using the Instead of Update Triggers in SQL Server. For this SQL Server Instead of UPDATE Triggers example, we are using the MERGE Statement.

CREATE TRIGGER InsteadOfUPDATETriggerExample on [EmployeeTable]
INSTEAD OF UPDATE 
AS DECLARE @ActionPeformed VARCHAR(50)

IF UPDATE(YearlyIncome)
BEGIN
     SET @ActionPeformed = 'Updated Yearly Income'
END
ELSE BEGIN
      SET @ActionPeformed = 'Updated Sales'
END

MERGE [EmployeeTableAudit] AS AuditTab
USING (SELECT * FROM INSERTED) AS Emp
ON AuditTab.ID = emp.ID
WHEN MATCHED THEN
UPDATE SET AuditTab.[Name] = Emp.Name, 
         AuditTab.[Education] = Emp.Education, 
	 AuditTab.[Occupation] = Emp.Occupation,
	 AuditTab.[YearlyIncome] = Emp.YearlyIncome, 
	 AuditTab.[Sales] = Emp.Sales, 
	 AuditTab.[Update Time] = GETDATE(), 
	 AuditTab.[ActionPerformed] = @ActionPeformed;
PRINT 'We Successfully Fired Our Second INSTEAD OF UPDATE Triggers in SQL Server.'
GO
Instead Of UPDATE Triggers in SQL Server 8

Next, let me perform updating on the employee table

-- SQL INSTEAD OF UPDATE Triggers Example

UPDATE [EmployeeTable]
	SET [YearlyIncome] = 111111, 
	    [Sales] = 7777
	WHERE [Occupation] = N'Clerical'
Instead Of UPDATE Triggers in SQL Server 9

We don’t have to check the Employee table as we all know that there will be no updates happened in that table. Next, check with the Employee Audit table.

Instead Of UPDATE Triggers in SQL Server 10

From the above screenshot, you can see that the trigger has updated all the records.

Instead of UPDATE Triggers in SQL Server Example 3

This example shows you how to update all the rows in the Employee table using the Instead Of Update Triggers in SQL Server. For this, we are using the INNER JOIN.

-- Example for INSTEAD OF UPDATE Triggers in SQL Server

CREATE TRIGGER InsteadOfUPDATETriggerExample on [EmployeeTable]
INSTEAD OF UPDATE 
AS 

UPDATE [EmployeeTable] 
 SET [EmployeeTable].[Name] = 'Tutorial Gateway', 
     [EmployeeTable].[Education] = ins.Education,
     [EmployeeTable].[Occupation] = ins.Occupation,
     [EmployeeTable].[YearlyIncome] = ins.YearlyIncome,
     [EmployeeTable].[Sales] = 55555
 FROM [EmployeeTable] 
 INNER JOIN INSERTED AS ins
 ON [EmployeeTable].ID = ins.ID 

PRINT 'We Successfully Fired Our Third INSTEAD OF UPDATE Triggers.'
GO
Instead Of UPDATE Triggers in SQL Server 11

Below statement will set the name as ‘Tutorial gateway’

SET [EmployeeTable].[Name] = 'Tutorial Gateway',

Next statement will set the Sales amount as 55,555

 [EmployeeTable].[Sales] = 55555

It means, whatever you pass the values to the name, and Sales columns, Instead of Update Trigger in Sql Server will insert the ‘Tutorial gateway’, and 55,555. Or you can say, the trigger will override the values

Next, let me perform updating on the employee table

UPDATE [EmployeeTable]
	SET [YearlyIncome] = 999999, 
	    [Sales] = 88886
	WHERE [Occupation] = N'Management'
Instead Of UPDATE Triggers in SQL Server 12

From the above screenshot, our SQL instead of update trigger fired. And also updated all the rows in the employee table. Let us see the Employee table.

Instead Of UPDATE Triggers in SQL Server 13
Categories SQL