Subject | Re: [IBO] dublicate records |
---|---|
Author | Helen Borrie |
Post date | 2003-09-10T23:49:32Z |
At 12:13 AM 11/09/2003 +0200, you wrote:
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
>What is the best way to check for dublicate records? I want to have a form,Using dataset methods, the only way to find out whether a new record is a
>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.
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