Create a trigger system in SQL Server (Trigger After Update) | YourSite

Create a trigger system in SQL Server (Trigger After Update)

SQL SERVER • 937 views

Trigger After Update

Create a trigger system. When user will update the data from the table the data will insert into another table which is reserved for the backup and it should show the track information as Updated.

Database Name


USE DB02TEST01

Create a primary table which will store the basic information of the products


CREATE TABLE Product_RAS_1637935(
ProductId int,
ProductName varchar(50),
Price Money
)

This table will store the product information after updation of the data from the primary table


CREATE TABLE ProductLog_RAS_1637935(
ProductId int,
ProductName varchar(50),
Price Money,
Track varchar(50),
AuditTime datetime
)

In this section we are creating the TRIGGER for storing the information inside the log table. Also In this section you have to remember that you should to use the word AFTER DELETE


ALTER TRIGGER BALLupdate ON Product_RAS_1637935
AFTER UPDATE
AS
BEGIN
DECLARE @ProductId int
DECLARE @ProductName varchar(50)
DECLARE @Price Money
SET @ProductId = (SELECT I.ProductId FROM inserted I)
SET @ProductName = (SELECT I.ProductName FROM inserted I)
SET @Price = (SELECT I.Price FROM inserted I)
INSERT INTO ProductLog_RAS_1637935 VALUES (@ProductId, @ProductName, @Price, 'Updated',
getdate())
END

Before Updation the table should have some data inside table. So insert one row in the primary table


INSERT INTO Product_RAS_1637935 VALUES(100, 'Rice',100)

SELECT * FROM Product_RAS_1637935
SELECT * FROM ProductLog_RAS_1637935

After deletion of the information trigger will work. It will insert 1 more extra row


UPDATE Product_RAS_1637935 SET ProductName = 'Sugar' WHERE ProductId = 100

See the information in both the table


SELECT * FROM Product_RAS_1637935
SELECT * FROM ProductLog_RAS_1637935

🚀 More Blogs You Might Like

Explore more articles and keep learning

Heat Stroke in Summer: Causes, Symptoms, Prevention and What To Do
health
Heat Stroke in Summer: Causes, Symptoms, Prevention and What To Do

Heat Stroke in Summer: Causes, Symptoms, Prevention and What To Do...

👁 9 2026-04-26
Read More →
Alcoholic Fatty Liver Disease: Causes, Symptoms, Risks and How to Improve It
health
Alcoholic Fatty Liver Disease: Causes, Symptoms, Risks and How to Improve It

Alcoholic Fatty Liver Disease: Causes, Symptoms, Risks and How to Improve It...

👁 9 2026-04-26
Read More →
Non-Alcoholic Fatty Liver: Meaning, Causes, Symptoms, and How to Improve It
health
Non-Alcoholic Fatty Liver: Meaning, Causes, Symptoms, and How to Improve It

Non-Alcoholic Fatty Liver: Meaning, Causes, Symptoms, and How to Improve It...

👁 10 2026-04-26
Read More →
The Ultimate Blueprint to Score 70/70 in ISC Class 12 Computer Science
class-1-12-resources
The Ultimate Blueprint to Score 70/70 in ISC Class 12 Computer Science

The Ultimate Blueprint to Score 70/70 in ISC Class 12 Computer Science...

👁 47 2026-04-11
Read More →