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
 plz help ...trigger

Author  Topic 

wormz666
Posting Yak Master

110 Posts

Posted - 2008-10-05 : 21:48:44
i have a problem with the trigger.....
i dont want duplicate in my table.....

declare @lname varchar
declare @fname varchar

if Exists(Select lname,fname from sample1 where lname=@lname and fname=@fname)
begin
raiserror 14000 'Existing Record'
rollback transaction
end

please help....
thank you in advance

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-06 : 00:05:35
two approaches.
1.Easiest way is to set unique constraint on column where you want to avoid duplicate values
2.Another way is to use an INSTEAD OF TRIGGER

CREATE TRIGGER YourTrigger ON sample1
INSTEAD OF INSERT
AS
IF NOT EXISTS(Select lname,fname from sample1 where lname=@lname and fname=@fname)
INSERT INTO sample1
SELECT fields FROM INSERTED
Go to Top of Page
   

- Advertisement -