How to insert ?

  • Thread starter Thread starter ad
  • Start date Start date
A

ad

I want to insert about 10000 records into database.
I don't know if the records existed in the database at first.
There are to methods to achieve it.
1. Use select to test if primary key of a record existed in the database, if
not, use insert command to insert the record.
2. Do not test, just use the insert command to insert the record, if the
record existed, the Exception will be raised, ignore the exception.

Which method is better?
 
In general, I try to avoid exceptions so would opt for #1

A third option (depending on row size) would be to create a temp table with
the same structure as the target table, insert all 10000 records into the
temp table and then insert f1, f2, fn from temp where temptablepk not in (
select targetpk from targettable ) then finally drop the temp table.

Once all the records have been inserted into the temp table, no more traffic
on the wire until insert completes.
 
In addition to Jim's reply:

Exceptions are exceptions - something out of your control and shouldn't be
abused.
 
Back
Top