"AFTER" triggers are executed after the triggering events like INSERT, UPDATE or DELETE events, where as the "INSTEAD OF" triggers are fired in place of the triggering events. INSTEAD OF triggers are usually used to correctly update views that are based on multiple tables.
Steps to be followed to use INSTEAD OF INSERT trigger
So, first, let's create two tables named Student & Section.
So, first, let's create two tables named Student & Section.
Table Student SQL script
- CREATE TABLE Student
- (
- Id int Primary Key,
- Name nvarchar(30),
- Gender nvarchar(10),
- SectionId int
- )
- CREATE TABLE Section
- (
- SecId int Primary Key,
- SecName nvarchar(20)
- )
Then, insert some dummy records in both the tables.
- Insert into Section values (1,'Sec1')
- Insert into Section values (2,'Sec2')
- Insert into Section values (3,'Sec3')
- Insert into Section values (4,'Sec4')
- Insert into Student values (1,'Satya1', 'Male', 3)
- Insert into Student values (2,'Satya2', 'Male', 2)
- Insert into Student values (3,'Satya3', 'Female', 1)
- Insert into Student values (4,'Satya4', 'Male', 4)
- Insert into Student values (5,'Satya5', 'Female', 1)
- Insert into Student values (6,'Satya6', 'Male', 3)
Let's create a View based on the above mentioned tables.
The View should return Student Id, Name, Gender, SecName columns. So, it is obviously based on multiple tables.
The View should return Student Id, Name, Gender, SecName columns. So, it is obviously based on multiple tables.
Script to create the View
- Create view ViewStudentDetails
- as
- Select Id, Name, Gender, SecName
- from Student
- join Section
- on Student.SectionId = Section.SecId





Yauheni LeaniukPosted Jan 10, 2021, 4:59 PM
Notice that inside trigger body we are using JOIN which is equal to INNER JOIN but not LEFT JOIN. As a novice with triggers I'm trying to do this with LEFT JOIN instead and I stuck with this for a many hours, so maybe this will be helpful for someone