Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

DB2 Insert & Update in a single statement

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
View previous topic :: :: View next topic  
Author Message
Anand Kumar

New User


Joined: 29 Aug 2007
Posts: 24
Location: chennai

PostPosted: Fri May 08, 2009 7:06 pm    Post subject: DB2 Insert & Update in a single statement
Reply with quote

Hi,

I have a requirement in a COBOL/DB2 program such that if the primary key columns are present we need to update the table else we need to insert a record. Is it possible in a single statement.

If so can you please give me an example!! I have given the insert query below. DAY_KEY & MONTH_KEY are the primary keys of GWEXP table

Code:

 EXEC SQL                                       
    INSERT INTO GWEXP                         
    (DAY_KEY,MONTH_KEY,BIED_RNT_PCT,LAAT_RNT_PCT)       
    VALUES (:DCLGWDAT03.DAG-KEY,                 
            :W-MONTH-KEY-EUR           ,         
            :DCLRENTEPERC-CALL.PERC-BD,         
            :DCLRENTEPERC-CALL.PERC-LD)                                 
 END-EXEC                                       


Thanks,

Anand
Back to top
View user's profile Send private message

sushanth bobby

Senior Member


Joined: 29 Jul 2008
Posts: 1013
Location: India

PostPosted: Fri May 08, 2009 7:25 pm    Post subject:
Reply with quote

Anand,

Get to know what version of DB2 you are using. If its DB2 V9, MERGE statement is what you are looking for. BLACKLE it.

Sushanth
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Fri May 08, 2009 7:31 pm    Post subject:
Reply with quote

deleted by poster
Back to top
View user's profile Send private message
Anand Kumar

New User


Joined: 29 Aug 2007
Posts: 24
Location: chennai

PostPosted: Fri May 08, 2009 7:38 pm    Post subject:
Reply with quote

We are using 8.1 icon_neutral.gif
Back to top
View user's profile Send private message
sushanth bobby

Senior Member


Joined: 29 Jul 2008
Posts: 1013
Location: India

PostPosted: Fri May 08, 2009 7:47 pm    Post subject:
Reply with quote

Anand,

Start programming...

Happy Weekend,
Sushanth
Back to top
View user's profile Send private message
MFRASHEED

Active User


Joined: 14 Jun 2005
Posts: 186
Location: USA

PostPosted: Fri May 08, 2009 11:52 pm    Post subject:
Reply with quote

You would DELETE the row first and then INSERT it.
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Sat May 09, 2009 1:11 am    Post subject:
Reply with quote

Quote:
You would DELETE the row first and then INSERT it.


That would be a backwards approach.

depends on the statistical chance of the row already existing.

If a majority of the 'update' requests
(the trigger or transaction file driving the process)
would result in an update to an existing row,
then simply UPDATE and if a NOFIND (+100), then INSERT.

on the other hand

If a majority of the 'update' requests
(the trigger or transaction file driving the process)
would require an INSERT,
then simply INSERT and if a DUP-ROW (-803), then UPDATE.

and a last point:
To always DELETE first and then INSERT would in effect use more space.
I don't believe that an INSERT would reuse a DELETEd row's space.
(but there are others here that do know and can correct me)
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts -913/-911 Deadlock during UPDATE stat... NoSleep319 DB2 5 Fri Nov 18, 2016 12:37 am
No new posts How to update a portion of text in a ... Bill Woodger DFSORT/ICETOOL 25 Wed Nov 09, 2016 9:41 pm
No new posts Updating Cursor row withour using FOR... chandan.inst DB2 15 Tue Nov 08, 2016 11:17 am
No new posts Two Selects in a single select query Rohit Umarjikar DB2 1 Fri Nov 04, 2016 8:46 pm
No new posts Single COPY CICS TS datasets and when... Kyle Carroll CICS 2 Tue Oct 11, 2016 9:47 pm


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us