Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 trigger help...plz help...

Author  Topic 

wormz666
Posting Yak Master

110 Posts

Posted - 2008-09-21 : 22:17:34
please help me with the trigger.....
if the employee has already exist it will not be inserted on my table empmas,else if the name of the employee is not existing then it will be inserted......thank you in advance

begin
declare @lname varchar(20)
declare @fname varchar(20)
declare @mname varchar(20)
select @lname = e.lname,@fname = e.fname,@mname = e.mname

from inserted e

if exists (Select * from empmas where lname =@lname and fname =@fname and mname =@mname)
begin
raiserror 14001 'Existing Employee'
rollback transaction
end
end



khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-09-21 : 22:20:20
begin
declare @lname varchar(20)
declare @fname varchar(20)
declare @mname varchar(20)

select @lname = e.lname,@fname = e.fname,@mname = e.mname
from inserted e


if exists
(
select *
from empmas e inner join inserted i
on e.lname = i.lname
and e.fname = i.fname
and e.mname = i.mname
)
begin
raiserror 14001 'Existing Employee'
rollback transaction
end
end


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

wormz666
Posting Yak Master

110 Posts

Posted - 2008-09-21 : 22:29:44
thank you so much.................
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-09-21 : 22:30:52
Do take note that, inserted may contains more than on records. What you did is assumed only single record which may not be true.


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

wormz666
Posting Yak Master

110 Posts

Posted - 2008-09-22 : 01:20:14
thank for the advice...........
Go to Top of Page
   

- Advertisement -