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 

DB2 Merge version 9

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


Joined: 30 Dec 2002
Posts: 140
Topics: 40

PostPosted: Tue Nov 03, 2009 12:16 pm    Post subject: DB2 Merge version 9 Reply with quote

I have been looking at documentation for version 9 and found it a little lacking. I am wondering if any of you who are on DB2 version 9 have tried the following.

The syntax for the merge statement says that you can also return results from the merge by using a SELECT statement. I have been able to prove this using SPUFI.

I am attempting to do a merge using multiple rows. I am reading rows from a sequential file and loading arrays which are referenced in the merge using a COBOL program.

My question is how to return multiple rows from the merge? Do you build a cursor which contains the SELECT.....MERGE and then open the cursor and do a multi-row fetch? Has anyone attempted this?
Back to top
View user's profile Send private message
NASCAR9
Intermediate


Joined: 08 Oct 2004
Posts: 274
Topics: 52
Location: California

PostPosted: Tue Nov 03, 2009 12:30 pm    Post subject: Reply with quote

jim haire,
I have been testing the 'MERGE' statement and I can't find any reference in the DB2 V9.1 for z/OS documentation for a Select. Am I missing something? Question
_________________
Thanks,
NASCAR9
Back to top
View user's profile Send private message
jim haire
Beginner


Joined: 30 Dec 2002
Posts: 140
Topics: 40

PostPosted: Tue Nov 03, 2009 2:22 pm    Post subject: Reply with quote

Yes, that documentation is out there. I am also testing this. We have a "sandbox" area where we're testing the new features from DB2 version 9. I can give you a little background of what I have set up.

I created a table called "MASTER" which contains all the people who have purchased items from our company. I created another table called TXN which contains all the transactions which have come in for the day.

I created the query below as an example of the merge and ran it in SPUFI. If the account number 1010101013 already exists in the database, the amount (101010.10) gets added to whatever amount exists for the record currently. If the account number does not exist, the record is inserted.

The column ACTION is returned as "UPD" if an update occurred. It returns an "INS" if the record didn't exist. Hope this helps explain how the SELECT works.

Code:
SELECT ACCT, FIRST_NAME, LAST_NAME, TOTAL_TXN, ACTION FROM FINAL TABLE
(MERGE INTO MASTER AS M      INCLUDE(ACTION   CHAR(3))
    USING (VALUES ('1010101013', 'JOANNE', 'TENPINS',101010.10))
      AS TXN (ACCT, FIRST_NAME, LAST_NAME, TXN_AMT)
   ON M.ACCT = TXN.ACCT
 WHEN MATCHED THEN
     UPDATE SET TOTAL_TXN = TOTAL_TXN + TXN.TXN_AMT
               ,ACTION = 'UPD'
 WHEN NOT MATCHED THEN
   INSERT (ACCT, FIRST_NAME, LAST_NAME, TOTAL_TXN, ACTION)
   VALUES (TXN.ACCT, TXN.FIRST_NAME, TXN.LAST_NAME, TXN.TXN_AMT, 'INS')
  NOT ATOMIC CONTINUE ON SQLEXCEPTION);
Back to top
View user's profile Send private message
NASCAR9
Intermediate


Joined: 08 Oct 2004
Posts: 274
Topics: 52
Location: California

PostPosted: Tue Nov 03, 2009 4:27 pm    Post subject: Reply with quote

Thanks jim haire! I applied your Select to my test query and it worked! 8)
I learned something today. Very Happy
_________________
Thanks,
NASCAR9
Back to top
View user's profile Send private message
jim haire
Beginner


Joined: 30 Dec 2002
Posts: 140
Topics: 40

PostPosted: Wed Nov 04, 2009 12:08 pm    Post subject: Reply with quote

Glad I could help. But my question hasn't been answered yet. Sad

To go a little further in explaining what I'm trying to do, let me put my SQL from my COBOL program below.

:ACCT-TAB, :FIRST-NAME-TAB, :LAST-NAME-TAB, and :TXN-AMOUNT-TAB are all arrays I have defined in my program. They each occur 1000 times. I read records from a sequential file and load values from the file into each array. I add 1 to :WS-ROW-COUNT for each record I read.

The documentation I've found said I should be able to do this and merge 1000 records at a time. However, I haven't found any documentation or examples which explain how I can return the results of these 1000 merges through the SELECT. It seems to me like this might be a cursor/fetch scenario. Just not sure...

Code:
SELECT ACCT
      ,FIRST_NAME
      ,LAST_NAME
      ,TOTAL_TXN
      ,ACTION
  FROM FINAL TABLE
(MERGE INTO MASTER AS M      INCLUDE(ACTION CHAR(3))
 USING (VALUES (:ACCT-TAB,:FIRST-NAME-TAB,
                :LAST-NAME-TAB,:TXN-AMOUNT-TAB)
   FOR :WS-ROW-COUNT ROWS)
    AS TXN (ACCT, FIRST_NAME, LAST_NAME, TXN_AMT)
    ON M.ACCT = TXN.ACCT
WHEN MATCHED THEN
    UPDATE SET TOTAL_TXN = TOTAL_TXN + TXN.TXN_AMT
              ,ACTION = 'UPD'
WHEN NOT MATCHED THEN
    INSERT (ACCT, FIRST_NAME, LAST_NAME, TOTAL_TXN, ACTION)
    VALUES (TXN.ACCT
           ,TXN.FIRST_NAME
           ,TXN.LAST_NAME
           ,TXN.TXN_AMT
           ,'INS')
 NOT ATOMIC CONTINUE ON SQLEXCEPTION)
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


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

PostPosted: Wed Nov 04, 2009 1:48 pm    Post subject: Reply with quote

jim haire,

I can't test , it but how about this?

Code:

SELECT Z.*                                                           
  FROM (MERGE INTO MASTER AS M INCLUDE(ACTION CHAR(3))               
        USING (VALUES (:ACCT-TAB,:FIRST-NAME-TAB,                     
                       :LAST-NAME-TAB,:TXN-AMOUNT-TAB)               
          FOR :WS-ROW-COUNT ROWS)                                     
           AS TXN (ACCT, FIRST_NAME, LAST_NAME, TXN_AMT)             
                   ON M.ACCT = TXN.ACCT                               
         WHEN MATCHED THEN                                           
              UPDATE SET TOTAL_TXN = TOTAL_TXN + TXN.TXN_AMT         
                        ,ACTION    = 'UPD'                           
         WHEN NOT MATCHED THEN                                       
              INSERT (ACCT, FIRST_NAME, LAST_NAME, TOTAL_TXN, ACTION)
              VALUES (TXN.ACCT                                       
                     ,TXN.FIRST_NAME                                 
                     ,TXN.LAST_NAME                                   
                     ,TXN.TXN_AMT                                     
                     ,'INS')                                         
         NOT ATOMIC CONTINUE ON SQLEXCEPTION) Z                       
Back to top
View user's profile Send private message Send e-mail Visit poster's website
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