In this article we will see that how we can insert data from one table to another using ‘TRIGGER’ function in SQL server.
i. customers
ii. orders
create table customers(
customer_id int primary key,
first_name varchar(50)
)
create table orders(
order_id int primary key identity,
price int,
customer_id int
)
insert into customers values(1,’John’),
(2,’Robert’),
(3,’David’),
(4,’John’),
(5,’Betty’)
create trigger tri_ord
on customers
for insert
as
begin
Declare @customer_id int
select @customer_id = customer_id from inserted
insert into orders(customer_id) values(@customer_id)
print ‘Inserted successfully’
end
go
insert into customers values(10,’Amar’)