Click here to Skip to main content
15,919,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i want to create a trigger on my table on insert.
description: i want : when i insert record to the table the same record insert to another table how can i access the values of the record in my trigger.
have your excuse on me because of language.thanks
Posted

create table tbl_login
(
userid varchar(20),
pwd varchar(20)
)

create table emp
(
fname varchar(30)not null,
mname varchar(30) not null,
lname varchar(30) not null,
gender varchar(15)not null,
dept varchar(30) not null,
design varchar(50) not null,
salary money,
email varchar(30)not null,
DOB varchar(30)not null,
Address varchar(50)not null,
country varchar(50)not null,
tel_res varchar(16)not null,
tel_ext varchar(16)not null,
mobile varchar(12)not null,
userid varchar(20) primary key,
pwd varchar(20) not null
)

-------------trigger for emp and tbl_login (trg_insert_user)-------------------------
create trigger trg_insert_user
on emp
for insert
as
declare
@userid varchar(20),
@pwd varchar(20)
set @userid=(select userid from inserted)
set @pwd=(select pwd from inserted)

insert into tbl_login
values(@userid,@pwd)

as soon as emp record is added then user id and pwd will automatically save to tbl_login
 
Share this answer
 
What you're interested in is the "virtual" INSERTED table. Read about it in this article: http://www.devarticles.com/c/a/SQL-Server/Using-Triggers-In-MS-SQL-Server/1/[^]. There are some examples on that page that should help you get started.

Best Regards,

-MRB
 
Share this answer
 
You can use inserted table...it will make the code simple

create trigger trg_insert_user
on emp
for insert
as
BEGIN
INSERT INTO tbl_login (userid,pwd)
SELECT userid,pwd from INSERTED
END


Thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900