What's a best-practices way to perform an Insert/Update?

  • Thread starter sherifffruitfly
  • Start date
S

sherifffruitfly

Hi all,

Given key cols/data cols, I want to:

IF record with that key exists, UPDATE the record with new data value,
IF record with that key does NOT exist, insert the record.

Oracle has a MERGE command, which may be used for this purpose, but
that's not portable. What's a portable good adonet way to do this?

Thanks,

cdj
 
E

Earl

I use the simple technique of checking for an existing positive primary key
as compared to a negative dataset-generated primary key. Set your
auto-generated dataset primary key columns to -1 seed and -1 increment.

Pseudo-code:

If Pri-Key > 0 then
Update
Else
Insert
End if
 
J

Jim Brandley

That works, but requires two round trips to the DB server. I would use a
stored procedure and do it in one round trip.
 
E

Earl

Nope, just uses the existing data in the dataset -- you either update or
insert (assuming the decision is based upon just one record). However, I've
also done it with a stored procedure transaction, but found that it didn't
fit in as neatly with the whole strongly-typed approach. If you were
updating and inserting a batch, the transaction would be the better way to
go.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top