Solved

Stop trigger from inserting new row if ID already exists

Posted on 2007-11-14
5
1,112 Views
Last Modified: 2008-02-01
For some reason we are getting duplicate values inserted into a table with the same foreign key.

As an interim solution i need to stop this happening via a trigger.

Could someone knock up a quick trigger which checks for an ID and if it exists it cancels the insert statement.

Note: The insert statement isn't inside the trigger
0
Comment
Question by:Type25
  • 3
  • 2
5 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20278554
maybe putting a unique index (or primary key) on the ID field would even be better, as it would give you the indication where/how these duplicates get inserted...

what do you think?
ie, how do you "generate" those ID values?
0
 
LVL 9

Author Comment

by:Type25
ID: 20278589
angelll, i'll be doing this after, for a really quick fix i need to put a trigger in place.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20278613
do you have a primary key on the table?
0
 
LVL 9

Author Comment

by:Type25
ID: 20278621
Table is like this:

EOWID int primary key
JobNo foreign key
other fields.....

We're getting duplicate job no's.
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 20278660
create trigger trg_dup_jobno
on yourtable
for insert
as
  if exists ( select null from yourtable t group by t.jobno having count(*) > 1 )
  begin
    rollback
  end
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question