Updated on Kisan Patel
This tutorial will show you how to create AFTER INSERT triggers in SQL Server?
AFTER INSERT trigger executes after a record is inserted into the database. Open a new query window and write below statements
CREATE TRIGGER InsertNewTrigger ON [NORTHWND].[dbo].[Region] AFTER INSERT AS BEGIN DECLARE @id int SELECT @id = [RegionID] FROM inserted INSERT INTO [Region] (RegionDescription) VALUES ('New') END
Notice the above code snippet, the name of the trigger is
InsertNewTrigger and created on Region table. The type of this trigger is “AFTER INSERT”. Inside the trigger, we have declared a variable and trying to get the
RegionID primary key value of the inserted table and the same is being used to insert a record into Region table.
To test, whether this Trigger is working, try to insert a new record into
INSERT INTO [Region](RegionDescription) VALUES ( 'OLD')
This insert a record into PersonalDetails table and a record into Accounts table (two records).
To delete any trigger created, simply right click the trigger name and choose Delete and click OK on the dialog box. It can also be done using
DROP TRIGGER <trigger name> statement running in the query window.