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 version 8 Multi row insert

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


Joined: 24 Mar 2006
Posts: 32
Topics: 16

PostPosted: Thu May 13, 2010 9:06 pm    Post subject: DB2 version 8 Multi row insert Reply with quote

Have read that multi row insert is around 15% more effiect than normal inserts ,am trying to implement it ,in one of our programs but am not sure how to use the get diagonostic for error handling...
Below is the code ,Can anyone suggest if there are any special SQL code that needs to be handled.Also how can i handle duplicate records here..i don't want the program to error for duplicate records...

Code:


EXEC SQL                                     
       INSERT INTO CUST_INCL_ON_RQST             
           (STMT_RQST_ID                         
          , CUSTOMER_ID                         
          , SMPL_CUST_YN                         
          , RSLT_CD)                             
       VALUES                                   
           (:ZNHDCUIR-STMT-RQST-ID               
          , :WS-CUSTOMER-ID                     
          , :ZNHDCUIR-SMPL-CUST-YN               
          , :ZNHDCUIR-RSLT-CD)                   
            FOR :NUM-ROWS ROWS                   
            NOT ATOMIC CONTINUE ON SQLEXCEPTION                             
   END-EXEC.                                     
                                     
    IF SQLCODE = +0                                         
           MOVE  ROW-COUNT       TO WS-CUST-INSRTD-CNT       
    ELSE                                                     
      MOVE 1 TO CTR                                         
         PERFORM VARYING CTR FROM 1 BY 1  UNTIL CTR>NUM-ROWS
           EXEC SQL                                         
              GET DIAGNOSTICS CONDITION  :CTR               
                :ERR-SQLCODE  = DB2_RETURNED_SQLCODE,       
                :ERR-SQLSTATE = RETURNED_SQLSTATE,           
                :ROW-NUM      = DB2_ROW_NUMBER               
           END-EXEC                                         
      ADD 1 TO CTR                                           
       IF  ERR-SQLCODE NOT EQUAL 0  OR -803           
        INITIALIZE        ERROR-DTL               
        MOVE  ERR-SQLCODE TO WS-ERR-CODE           
        MOVE  ERR-SQLSTATE TO WS-ERR-SQLSTATE     
        Move WS-CUSTOMER-ID (DB2_ROW_NUMBER) TO WS-ERR-CUSTOMER
     
       WRITE ERR-RECORD
        PERFORM ABEND-PROGRAM
        END-IF
END-PERFORM
Back to top
View user's profile Send private message Send e-mail
papadi
Supermod


Joined: 20 Oct 2009
Posts: 594
Topics: 1

PostPosted: Thu May 13, 2010 10:50 pm    Post subject: Reply with quote

Quote:
i don't want the program to error for duplicate records...
I probably misunderstand, but yes you do want the program to error if the duplicate is detected. . .
_________________
All the best,

di
Back to top
View user's profile Send private message
Anuj Dhawan
Intermediate


Joined: 19 Jul 2007
Posts: 298
Topics: 7
Location: Mumbai,India

PostPosted: Fri May 14, 2010 4:22 am    Post subject: Reply with quote

you might like to look here: http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=/com.ibm.db2.doc.relg/fmlt.htm
_________________
Regards,
Anuj
Back to top
View user's profile Send private message
nishantrk
Beginner


Joined: 24 Mar 2006
Posts: 32
Topics: 16

PostPosted: Fri May 14, 2010 6:49 am    Post subject: Reply with quote

papadi wrote:
Quote:
i don't want the program to error for duplicate records...
I probably misunderstand, but yes you do want the program to error if the duplicate is detected. . .


yeah..I don't want the program to abend ,if duplicates are present ,the code should have been as below.




Code:
IF SQLCODE = +0                                 
           MOVE  ROW-COUNT       TO WS-CUST-INSRTD-CNT       
    ELSE                                                     
      MOVE 1 TO CTR                                         
         PERFORM VARYING CTR FROM 1 BY 1  UNTIL CTR>NUM-ROWS
           EXEC SQL                                         
              GET DIAGNOSTICS CONDITION  :CTR               
                :ERR-SQLCODE  = DB2_RETURNED_SQLCODE,       
                :ERR-SQLSTATE = RETURNED_SQLSTATE,           
                :ROW-NUM      = DB2_ROW_NUMBER               
           END-EXEC                                         
      ADD 1 TO CTR                                           
       IF  ERR-SQLCODE NOT EQUAL 0  AND -803         
       
 INITIALIZE        ERROR-DTL               
        MOVE  ERR-SQLCODE TO WS-ERR-CODE           
        MOVE  ERR-SQLSTATE TO WS-ERR-SQLSTATE     
        Move WS-CUSTOMER-ID (DB2_ROW_NUMBER) TO WS-ERR-CUSTOMER
     
       WRITE ERR-RECORD
        PERFORM ABEND-PROGRAM
        END-IF
END-PERFORM
Back to top
View user's profile Send private message Send e-mail
Dibakar
Advanced


Joined: 02 Dec 2002
Posts: 700
Topics: 63
Location: USA

PostPosted: Mon May 17, 2010 12:34 pm    Post subject: Reply with quote

Code:
PERFORM VARYING CTR FROM 1 BY 1  UNTIL CTR>NUM-ROWS
           EXEC SQL                                         
              GET DIAGNOSTICS CONDITION  :CTR 


CTR is not a row number, but a condition number. See example 6 here

I get confused how the AND is handled with NOT so for non duplicate errors i would code
Code:
IF  (ERR-SQLCODE NOT EQUAL 0)  AND
     (ERR-SQLCODE NOT EQUAL -803)

instead of
Code:
IF  ERR-SQLCODE NOT EQUAL 0  AND -803
Back to top
View user's profile Send private message Send e-mail
nishantrk
Beginner


Joined: 24 Mar 2006
Posts: 32
Topics: 16

PostPosted: Mon May 17, 2010 7:37 pm    Post subject: Reply with quote

Thanks Dibankar...

yeah i had changed my code ..but still its doesn't seem to work perfectly...
I changed it to..

Code:
IF SQLCODE = +0 
add to inserted-counter

else

IF SQLCODE = -253                       
INITIALIZE DB2RC                     
ROW-COUNT                 
ERRCNT                   
EXEC SQL
GET DIAGNOSTICS
 :ROW-COUNT = ROW_COUNT,
 :ERRCNT= NUMBER 
 END-EXEC
 MOVE 1 TO CTR
PERFORM VARYING CTR FROM 1 BY 1
UNTIL CTR > ERRCNT
 EXEC SQL
 GET DIAGNOSTICS CONDITION  :CTR
:ERR-SQLCODE  = DB2_RETURNED_SQLCODE, 
  :ERR-SQLSTATE = RETURNED_SQLSTATE,
  :ROW-NUM = DB2_ROW_NUMBER
 
IF  (ERR-SQLCODE  EQUAL 0 ) OR
(ERR-SQLCODE EQUAL -803)
CONTINUE
ELSE
WRITE CUSTOMER (ROW-NUM) TO ERR-
FILE
ENDIF
 
END-EXEC
 END-PERFORM
END-IF
Back to top
View user's profile Send private message Send e-mail
nishantrk
Beginner


Joined: 24 Mar 2006
Posts: 32
Topics: 16

PostPosted: Mon May 17, 2010 7:55 pm    Post subject: Reply with quote

Sorry the earlier got posted half way through...
This code is working fine for me..the only problem am getting is
...the number of conditions returned in the ERRCNT is coming as 2 ,whereas i have only one error in my insert.

I mean it is taking the SQLCODE -253 (which comes if there are more than one row in error) as the first condition and -803 as the 2nd condition.
Is this how its suppose to work or am missing something...




Code:
IF SQLCODE = +0 
   add to inserted-counter

else

IF SQLCODE = -253                       
INITIALIZE DB2RC                     
                 ROW-COUNT                 
                ERRCNT                   
EXEC SQL
GET DIAGNOSTICS
    :ROW-COUNT = ROW_COUNT,
     :ERRCNT= NUMBER 
END-EXEC

MOVE 1 TO CTR

PERFORM VARYING CTR FROM 1 BY 1
UNTIL CTR > ERRCNT
 EXEC SQL
 GET DIAGNOSTICS CONDITION  :CTR
      :ERR-SQLCODE  = DB2_RETURNED_SQLCODE, 
      :ERR-SQLSTATE = RETURNED_SQLSTATE,
      :ROW-NUM = DB2_ROW_NUMBER
 END-EXEC
 
IF  (ERR-SQLCODE  EQUAL 0 ) OR
(ERR-SQLCODE EQUAL -803)
      CONTINUE
ELSE
      WRITE CUSTOMER (ROW-NUM) TO ERR- FILE
ENDIF
 
 END-PERFORM
END-IF
Back to top
View user's profile Send private message Send e-mail
Dibakar
Advanced


Joined: 02 Dec 2002
Posts: 700
Topics: 63
Location: USA

PostPosted: Tue May 18, 2010 11:11 am    Post subject: Reply with quote

-253 means some rows were successful and some not. It doesn't say anything about more than one.

ERRCNT equals 2 means that there are two errors, they can be -803 but not -253.

To see all the error codes debug or change -

Code:
IF  (ERR-SQLCODE  EQUAL 0 ) OR
(ERR-SQLCODE EQUAL -803)
      CONTINUE
ELSE
      WRITE CUSTOMER (ROW-NUM) TO ERR- FILE
ENDIF

to

Code:

IF  (ERR-SQLCODE  NOT EQUAL 0 )
  WRITE CUSTOMER (ROW-NUM) ERR-SQLCODE  ROW-NUM TO ERR- FILE
ENDIF
Back to top
View user's profile Send private message Send e-mail
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