Portal | Manuals | References | Downloads | Info | Programs | JCLs | Mainframe wiki | Quick Ref
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist 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: 6968
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: 6968
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 Insert a Row_number into table useit DB2 2 Tue Sep 19, 2017 1:07 pm
No new posts More than 1 Include criteria for sing... Roy Ware SYNCSORT 6 Wed Sep 13, 2017 11:45 pm
No new posts WER268A OUTREC STATEMENT : SYNTAX E... frozenblood87 SYNCSORT 12 Sat Aug 26, 2017 9:45 pm
No new posts Suppress value reported by BREAK in D... Learncoholic DFSORT/ICETOOL 3 Wed Aug 16, 2017 6:03 pm
No new posts Multiple VSAM files single output fil... Mohan Kothakota DFSORT/ICETOOL 11 Wed Aug 09, 2017 7:57 pm

Facebook
Back to Top
 
Job Vacancies | Forum Rules | Bookmarks | Subscriptions | FAQ | Polls | Contact Us