INSTEAD OF DELETE triggers are used to delete records from a View that is based on multiple tables.
Description
An INSTEAD OF DELETE trigger gets executed in place of the DELETE event on a table or a View. We keep an INSTEAD OF DELETE trigger on a View or a table, and when we try to update a row from that View or table, instead of the actual DELETE event, the trigger gets fired automatically.
Note about Instead of Insert,Instead of Delete,Instead of Update
Instead of Insert -->> DELETED table is always empty and the INSERTED table contains the newly inserted data.
Instead of Delete -->> INSERTED table is always empty and the DELETED table contains the rows deleted.
Instead of Update -->> DELETED table contains OLD data before modify,and inserted table contains NEW data and Updated data.
Steps to follow
First, create two tables.
SQL Script to create tblEmployee1 table
First, create two tables.
SQL Script to create tblEmployee1 table
- CREATE TABLE tblEmployee1
- (
- Id int Primary Key,
- Name nvarchar(30),
- Gender nvarchar(10),
- DepartmentId int
- )
- CREATE TABLE tblDepartment1
- (
- DeptId int Primary Key,
- DeptName nvarchar(20)
- )
- Insert into tblDepartment1 values (1,'Blog')
- Insert into tblDepartment1 values (2,'Article')
- Insert into tblDepartment1 values (3,'Resource')
- Insert into tblDepartment1 values (4,'Book')
- Insert into tblEmployee1 values (1,'Satya1', 'Male', 3)
- Insert into tblEmployee1 values (2,'Satya2', 'Male', 2)
- Insert into tblEmployee1 values (3,'Satya3', 'Female', 1)
- Insert into tblEmployee1 values (4,'Satya4', 'Male', 4)
- Insert into tblEmployee1 values (5,'Satya5', 'Female', 1)
- Insert into tblEmployee1 values (6,'Satya6', 'Male', 3)
- select * from tblEmployee1
- select * from tblDepartment1

Create a View based on these tables. The View should return Employee Id,Employee Name,Employee Gender and Employee DepartmentName columns so that it can be based on multiple tables.
Script to create the view
Script to create the view
- Create view ViewEmployeeDetails1
- as
- Select Id, Name, Gender, DeptName
- from tblEmployee1
- join tblDepartment1
- on tblEmployee1.DepartmentId = tblDepartment1.DeptId





Yauheni LeaniukPosted Jan 10, 2021, 5:01 PM
Thanx for the good articles about triggers, very clear and understandable!