View previous topic :: View next topic |
Author |
Message |
danm Intermediate
Joined: 29 Jun 2004 Posts: 170 Topics: 73
|
Posted: Fri Aug 13, 2004 10:41 am Post subject: Insert/Update a row |
|
|
If a row (by unique index) exists in the table, I want to update the row, otherwise insert a new row. There are 3 ways to accomplish this:
1. a. Update the row
b. If sqlcode = 100 (no row found), insert a new row
2. a. Insert a new row
b. If sqlcode = -803 (duplicate key), update the row
3. a. Select from the table with FOR UPDATE clause
b. If sqlcode = 0, update the row at the cursor
c. If sqlcode = 100 (no row found), insert a new row
Which way will give better performance? |
|
Back to top |
|
|
kolusu Site Admin
Joined: 26 Nov 2002 Posts: 12376 Topics: 75 Location: San Jose
|
|
Back to top |
|
|
blitz Beginner
Joined: 24 Dec 2002 Posts: 28 Topics: 4
|
Posted: Tue Jan 16, 2007 9:41 am Post subject: |
|
|
If the table to be updated has triggers defined, and you do not want to 'update' if there isn't going to be any change in data, then option '3' is the best way out as options '1' and '2' might trigger unwantedly if updated blindly.
sorry for digging out skeletons from the graveyard, but I just wanted to put my thoughts in for people using the search facility in these boards |
|
Back to top |
|
|
dbzTHEdinosauer Supermod
Joined: 20 Oct 2006 Posts: 1411 Topics: 26 Location: germany
|
Posted: Tue Jan 16, 2007 10:17 am Post subject: |
|
|
Sorry to be a 'mister precision', but danm, you question is worded for failure. What are you doing? Is this a business application where you are posting transactions against a master or are you doing a table load? What are the volumes involved? If you would explain what business process you are attempting to accomplish and some details (such as blitz brought up - are there triggers involved with the table) you will get a better answer.
you cannot choose a methodology if you don't know the process. _________________ Dick Brenholtz
American living in Varel, Germany |
|
Back to top |
|
|
blitz Beginner
Joined: 24 Dec 2002 Posts: 28 Topics: 4
|
Posted: Wed Jan 17, 2007 1:34 am Post subject: |
|
|
blitz wrote: | If the table to be updated has triggers defined, and you do not want to 'update' if there isn't going to be any change in data, then option '3' is the best way out as options '1' and '2' might trigger unwantedly if updated blindly.
|
Another alternative for lazybones like me is something like this,
Code: |
UPDATE DSN8810.EMP
SET PHONENO = '3565'
WHERE NOT EXISTS
(SELECT PHONENO
FROM DSN8810.EMP
WHERE PHONENO='3565'
AND EMPNO='000190'
AND ...other key information)
AND EMPNO='000190'
AND ... other key information
|
This avoids the unwanted-trigger problem. |
|
Back to top |
|
|
|
|