A trigger is a special stored procedure that fires automatically when data modification occurs against a table.
How to Create a Trigger:
Firstly create your table to insert data:
create table tableA
(
id int identity(1,1) primary key,
name varchar(25),
age int check(age>18),
server_date datetime default getdate()
)
Create your second table that affects when trigger fires:
create table tableB
(
id int primary key,
insert_date datetime
)
Now following will be the structure of your trigger for insertion. Here “myfirsttrigger” is the name of the trigger.
create trigger myfirsttrigger on tableA
for insert
as
begin
declare @id int
select @id=id from inserted
insert into tableB (id,insert_date) values(@id,getdate());
end