Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ignore duplicate key error and continue

Status
Not open for further replies.

lespaul

Programmer
Feb 4, 2002
7,083
US
I have an insert query that, if it returns a duplicate key error, instead of informing the user of the error, ignore it and keep processing the code. Is this possible?

Thanks!

leslie
 
Hi,

you can manage exceptions (error) with the statement

Try
Query1.ExecSQL;
Except
ShowMEssage('Ignore ERROR');
End;
---
This as generic suggestion,
*** BUT ***
Maybe you'll better write a SQL statement that's insert records avoiding duplications,
for example

Insert into Old_Customer
Select * From Customer
Where Not Exists(Select * From Old_Customer
Where Old_Customer.id=Customer.id)


Hope this can be usefully.

Ciao,
Geppo Darkson.

 
Actually what I'm doing is continuing a trial to a second or more day. So the program inserts a new trial slot into the table. But, there are other ways to get a new slot. So if the user has already created a slot in one of the other processes, I don't need one. I have worked around it by running a query to see if a record exists, and if not, then insert one, but it's just one more query instead of just passing over the error.

Thanks!
Leslie
 
If you remove the keyfield property it will insert the record, but that will violate data inconsistency. It would be better to make the Number record read-only and allow only editing of the remaining fields

Steven van Els
SAvanEls@cq-link.sr
 
You can try the following, but generally keys are not meant to be broken. Check your schema and design to find out why collisions are possible.

Note: you can also use OnPostError

Code:
...
while True;
try
    Post;
    Break;
except
    on E:EDBEngineError do
    begin
        if IsKeyViolation(E) then
            DataSet.FieldByName('DATE_POSTED').AsDateTime := Now
        else
             raise;
    end;
end;

function IsKeyViolation(E : Exception) : Boolean;
begin
   if (E is EDBEngineError) and (EDBEngineError(E).ErrorCount > 0) and
       (EDBEngineError(E).Errors[0].ErrorCode = DBIERR_KEYVIOL) then
   begin
       Result := True;
   end
   else
       Result := False;
end;

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top