Subject Re: [IBO] dublicate records
Author Helen Borrie
At 12:13 AM 11/09/2003 +0200, you wrote:
>What is the best way to check for dublicate records? I want to have a form,
>where user could enter pair's info - if members of pair doesn't exist, they
>are saved in DB; if such record already exist, posting to DB is prevented.

Using dataset methods, the only way to find out whether a new record is a
duplicate is to
1) design your table so that it protects itself from duplicates (use a
unique constraint if the primary key is not sufficient)
and then
2) try to post the record and trap the uniqueness violation exception.

In SQL, the way to find out whether there is a duplicate in the *current
transaction's view of database state*, is to test whether a record already
exists containing the uniqueness condition, e.g.

select 1 from TheTable
where UniqueColumn = :uniquecolumn

In a stored procedure, you can do

if (exists (select 1 from TheTable where UniqueColumn = :uniquecolumn)) then
<perform update OR exit>;
else
<perform insert>

But another transaction could create a duplicate after your test...so it's
generally safest to let table constraints and exceptions take care of
existence testing - meaning you have to Post before you can find out
whether there will be a uniqueness violation.

Helen