Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ON Insert Trigger

Status
Not open for further replies.

crazydeveloper

Technical User
Apr 20, 2001
5
0
0
SG
hi friends,

pl. help me writing the Trigger while inserting into the table.


i want like this,

EMP_Table Finance_Table Marketing_Table


when i am inserting into emp_talbe, columns like this
empid,ename,dname,sal etc.

values(1,'SQL','Finance',1000)
values(2,'SERVER','Marketing',2000)

if dname finance i want to insert the related values into Finance and if it's marketing i want to insert into the Marketing table.

for inserting into 3 tables i want a write trigger and while inserting i want to check the value of the dname then i want to insert the same into appropiate table.


thanks in advance.




 
CREATE TRIGGER ins_trig
ON EMP_Table
FOR INSERT
AS

If inserted.dname='FINANCE'
Insert Finance_Table Values(c1,c2 ....)
Else
If inserted.dname='MARKETING'
Insert Marketing_Table Values(c1,c2 ....)


NOTE: "inserted" is a special table available in triggers.

Read more about triggers in SQL Books Online under CREATE TRIGGER (T-SQL). Terry
 
The T-SQL for the trigger in my previous post has incorrect syntax. This is the corrected version.

CREATE TRIGGER ins_trig
ON EMP_Table
FOR INSERT
AS

Declare @dname varchar(12)

Select @dname=dname From inserted

If @dname='FINANCE'
Insert Finance_Table Values(c1,c2 ....)
Else
If @dname='MARKETING'
Insert Marketing_Table Values(c1,c2 ....)
Terry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top