MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Insert/Update a row

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
danm
Intermediate


Joined: 29 Jun 2004
Posts: 170
Topics: 73

PostPosted: Fri Aug 13, 2004 10:41 am    Post subject: Insert/Update a row Reply with quote

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
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12376
Topics: 75
Location: San Jose

PostPosted: Fri Aug 13, 2004 10:54 am    Post subject: Reply with quote

Danm,

Option 1 & 2 are equally good. I would avoid the third option. Run an explain with option 1 and 2.

Check the access method used and choose the optimal access method. check this link for Using EXPLAIN to improve SQL performance

http://publibz.boulder.ibm.com/cgi-bin/bookmgr_OS390/BOOKS/DSNAPH11/6.4?DT=20010710165542

Hope this helps...

Cheers

kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
blitz
Beginner


Joined: 24 Dec 2002
Posts: 28
Topics: 4

PostPosted: Tue Jan 16, 2007 9:41 am    Post subject: Reply with quote

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
View user's profile Send private message
dbzTHEdinosauer
Supermod


Joined: 20 Oct 2006
Posts: 1411
Topics: 26
Location: germany

PostPosted: Tue Jan 16, 2007 10:17 am    Post subject: Reply with quote

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
View user's profile Send private message
blitz
Beginner


Joined: 24 Dec 2002
Posts: 28
Topics: 4

PostPosted: Wed Jan 17, 2007 1:34 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group