Re: Prevent insert duplicate record TRIGGER
From: Hari (hari_prasad_k_at_hotmail.com)
Date: 06/11/04
- Next message: Maria: "Finding tables referenced in a Stored Procedure"
- Previous message: Andre Ranieri: "Prevent insert duplicate record TRIGGER"
- In reply to: Andre Ranieri: "Prevent insert duplicate record TRIGGER"
- Messages sorted by: [ date ] [ thread ]
Date: Fri, 11 Jun 2004 10:14:41 +0530
Hi,
Use this script:-
Create TRIGGER tgPreventJobDupes ON [dbo].JOB FOR INSERT AS
DECLARE @InvoiceNo VarChar(9)
IF (SELECT count(*) FROM JOB , inserted WHERE job.InvoiceNo =
inserted.invoiceno)>1
begin
RollBack Transaction
end
else
commit tran
-- Thanks Hari MCDBA "Andre Ranieri" <Andre Ranieri@discussions.microsoft.com> wrote in message news:87187F42-4E07-4AA2-B5DE-00B207D7D6CF@microsoft.com... > > I'm hoping someone can help me refine a trigger which will prevent writing records to a table (dbo.JOB) if the field value of Inserted.InvoiceNo already exists in dbo.JOB.InvoiceNo. > > The syntax checks out on the trigger but I'm still able to write duplicate records into the table. I've been working on this most of the day, and am a bit baffled. > > Any suggestions would be much appreciated. > > Thanks, > > Andre Ranieri > > CREATE TRIGGER tgPreventJobDupes ON [dbo].JOB FOR INSERT AS > /* > InvioceNo is a varchar field in dbo.Job table. INSERT trigger prevents writing duplicate record if InvoiceNo field value already exists. > > */ > DECLARE @InvoiceNo VarChar(9) > SET @InvoiceNo = (SELECT TOP 1 InvoiceNo FROM INSERTED) > IF (SELECT COUNT(*) FROM JOB WHERE (InvoiceNo = @InvoiceNo)) != 0 > BEGIN > RollBack Transaction > End
- Next message: Maria: "Finding tables referenced in a Stored Procedure"
- Previous message: Andre Ranieri: "Prevent insert duplicate record TRIGGER"
- In reply to: Andre Ranieri: "Prevent insert duplicate record TRIGGER"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|
|