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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Remove Dups Trigger

Author  Topic 

jesus4u
Posting Yak Master

204 Posts

Posted - 2002-08-20 : 10:44:33
I have never written a trigger but is there a trigger that can detect on INSERT if dups are being written to the table? IF so can you please post a code example?

Thanks

jesus4u
Posting Yak Master

204 Posts

Posted - 2002-08-20 : 11:01:18
well what I did was check for existing records on the insert

DECLARE @C int

SELECT @C = count(*) FROM Responses WHERE Gid = @Gid AND msg = @msg AND Qid = @Qid AND ViewID = @ViewID

IF @C = 0


But is that an efficient way of doing things?

Thanks

Go to Top of Page

LarsG
Constraint Violating Yak Guru

284 Posts

Posted - 2002-08-20 : 11:17:35
No, rather

if exists (select 1 from join table2 on table1.componentid = table2.componentID )
begin
-- code
end

Why can't you add a unique constraint to the table instead?

Go to Top of Page

jesus4u
Posting Yak Master

204 Posts

Posted - 2002-08-20 : 11:26:29
quote:

No, rather

if exists (select 1 from join table2 on table1.componentid = table2.componentID )
begin
-- code
end

Why can't you add a unique constraint to the table instead?





Wouldn't it be

if NOT exists


unique constraint? Never heard of it.

Go to Top of Page

LarsG
Constraint Violating Yak Guru

284 Posts

Posted - 2002-08-20 : 11:42:20
quote:

unique constraint? Never heard of it.



If you are going to use databases, it's about time. Lok it up in BOL or on this page

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_ca-co_5t9v.asp

You can constraints to an existing table with an alter table statement.

Go to Top of Page
   

- Advertisement -