IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

DB2 Insert & Update in a single statement


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
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
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: 1020
Location: India

PostPosted: Fri May 08, 2009 7:25 pm
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
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
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: 1020
Location: India

PostPosted: Fri May 08, 2009 7:47 pm
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
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
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 View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts How to append a PS file into multiple... JCL & VSAM 3
No new posts Submit multiple jobs from a library t... JCL & VSAM 14
No new posts Insert header record with record coun... DFSORT/ICETOOL 14
No new posts Insert system time/date (timestamp) u... DFSORT/ICETOOL 5
No new posts Identify Program Insert DB2 7
Search our Forums:

Back to Top