0% found this document useful (0 votes)
58 views

Multi Row Fetch, INSERT and Get Diagnostics How To Put It Into Code

Uploaded by

Kalimuthu Sangar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

Multi Row Fetch, INSERT and Get Diagnostics How To Put It Into Code

Uploaded by

Kalimuthu Sangar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

SWG BetaWorks

Multi Row Fetch, INSERT and Get Diagnostics

How to put it into code

Paul Fletcher (Fletchpl@uk.ibm.com)

Multi-Row 4/21/2008 © 2007 IBM Corporation


SWG BetaWorks

Important Disclaimer
THE INFORMATION CONTAINED IN THIS PRESENTATION IS PROVIDED
FOR INFORMATIONAL PURPOSES ONLY.
WHILE EFFORTS WERE MADE TO VERIFY THE COMPLETENESS AND
ACCURACY OF THE INFORMATION CONTAINED IN THIS PRESENTATION, IT
IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED.
IN ADDITION, THIS INFORMATION IS BASED ON IBM’S CURRENT
PRODUCT PLANS AND STRATEGY, WHICH ARE SUBJECT TO CHANGE BY
IBM WITHOUT NOTICE.
IBM SHALL NOT BE RESPONSIBLE FOR ANY DAMAGES ARISING OUT OF
THE USE OF, OR OTHERWISE RELATED TO, THIS PRESENTATION OR ANY
OTHER DOCUMENTATION.
NOTHING CONTAINED IN THIS PRESENTATION IS INTENDED TO, OR
SHALL HAVE THE EFFECT OF:
• CREATING ANY WARRANTY OR REPRESENTATION FROM IBM (OR ITS
AFFILIATES OR ITS OR THEIR SUPPLIERS AND/OR LICENSORS); OR
• ALTERING THE TERMS AND CONDITIONS OF THE APPLICABLE
LICENSE AGREEMENT GOVERNING THE USE OF IBM SOFTWARE.

2 Multi-Row 4/21/2008 © 2007 IBM Corporation


SWG BetaWorks

Agenda

ƒ Multi-Row INSERT – What it is

ƒ How to change a program to use Multi-Row Insert

ƒ Multi-Row FETCH – What it is

ƒ How to code Multi-Row FETCH

3 Multi-Row 4/21/2008 © 2007 IBM Corporation


SWG BetaWorks

Multi-row INSERT What it is

ƒInserts multiple rows on one API call


ƒCan be ATOMIC or NOT ATOMIC
ƒCan be static or dynamic SQL
ƒSignificant performance boost

INSERT INTO T1 FOR :hv ROWS


VALUES( :ARRAY1, :ARRAY2) ATOMIC;

4 Multi-Row 4/21/2008 © 2007 IBM Corporation


SWG BetaWorks

What The Arrays Do NOT contain


Single Row Multi Row (2 rows)

COL1 COL1 (1)


COL2 (1)
COL2
COL3 COL3 (1)

COL4 COL4 (1)


COL1 (2)
COL2 (2)
COL3 (2)
COL4 (2)

5 Multi-Row 4/21/2008 © 2007 IBM Corporation


SWG BetaWorks

What The Arrays Do contain


Single Row Multi Row (2 rows)
COL1 (1) COL2 (1) COL3 (1) COL4 (1)
COL1
COL1 (2) COL2 (2) COL3 (2) COL4 (2)
COL2
COL3
COL4

There is an array for each column


NOT for each Row

6 Multi-Row 4/21/2008 © 2007 IBM Corporation


SWG BetaWorks

Setting your system to use the Sample programs

1. Create your own copy of SYSIBM.SYSTABLEPART – Remove the


Constraints to keep the testing simple
2. Use DSNTIAUL to Unload the data from SYSIBM.SYSTABLEPART
into a file
3. Programs PLFSRI08 (for V8) or PLFSRI09 (for DB2 9) will show how
to use Single row INSERTS to read a record from the file and
INSERT into your copy of SYSTABLEPART
4. Programs PLFMRI08 (for V8) and PLFMRI09 (for DB2 9) will show
how to read records from the file and INSERT 100 rows at a time
5. DCLGEN TPART8 (for V8) TPART9 (for DB2 9)
set STRUCTURE NAME to W-TPART
set FIELD NAME PREFIX to W-

7 Multi-Row 4/21/2008 © 2007 IBM Corporation


SWG BetaWorks

Single Row INSERT Program Logic

ƒ Reads one Record


ƒ Moves it into the storage created by the DCLGEN
ƒ Inserts a row
ƒ Loop around until end of file

8 Multi-Row 4/21/2008 © 2007 IBM Corporation


SWG BetaWorks

DCLGEN Used for single row INSERT

9 Multi-Row 4/21/2008 © 2007 IBM Corporation


SWG BetaWorks

Single Row Insert Code


READ FD-VB-FILE INTO FILE-INPUT
AT END MOVE 'Y' TO W-END-OF-FILE.
Read first record
PERFORM B000-PROCESS-RECORDS UNTIL END-OF-FILE.
GOBACK.
B000-PROCESS-RECORDS SECTION.
MOVE FILE-INPUT TO W-TPART.
***********************************************
Move the record to the DCLGEN
* INSERT ONE ROW AT A TIME
***********************************************
EXEC SQL
INSERT INTO SYSTABLEPART VALUES
(:W-PARTITION , Do the INSERT
:W-TSNAME ,
:W-DBNAME ,
…..
:W-RELCREATED )
END-EXEC.
READ FD-VB-FILE INTO FILE-INPUT
AT END MOVE 'Y' TO W-END-OF-FILE.
Read the next record

10 Multi-Row 4/21/2008 © 2007 IBM Corporation


SWG BetaWorks

Single Row INSERT Run JCL


ƒ //PLFSRI09 EXEC
PGM=IKJEFT01,DYNAMNBR=25,ACCT=SHORT,
ƒ // REGION=4096K
ƒ //STEPLIB DD DSN=SYS2.DB2.V910.SDSNLOAD,DISP=SHR
ƒ // DD DSN=SYS2.DB2.V910.SDSNEXIT.PIC,DISP=SHR
ƒ // DD DSN=FLETCHP.MASTER.LOAD,DISP=SHR
ƒ //DDSEQ01R DD DSN=FLETCHP.TPART.INPUT,DISP=SHR
ƒ //SYSUDUMP DD SYSOUT=*
ƒ //SYSTSPRT DD SYSOUT=*
ƒ //SYSOUT DD SYSOUT=*
ƒ //SYSABOUT DD SYSOUT=*
ƒ //SYSTSIN DD *
ƒ DSN SYSTEM(PB1I)
ƒ RUN PROGRAM(PLFSRI09) PLAN(PLFSRI09)
ƒ END
ƒ /*

File created by DSNTIAUL


11 Multi-Row 4/21/2008 © 2007 IBM Corporation
SWG BetaWorks

Multi-Row INSERT Program Logic


ƒ A new Copy Member is included TPART28 or TPART29
ƒ DCLGEN will not create a member for Multi-Row
– A Rexx Edit Macro (OCC) provides this function
ƒ Read a record
ƒ Move it into the storage created by the single occurrence
DCLGEN
ƒ Move each field into the next occurrence of that field in the
multiple occurrence DCLGEN
ƒ Once 100 records have been read INSERT them using Multi-
Row INSERT
ƒ Loop until end of file

12 Multi-Row 4/21/2008 © 2007 IBM Corporation


SWG BetaWorks

2 Errors Encountered

On one DB2 9 system Precompiler


failed with
HOST VARIABLE ARRAY "W-
PARTITION" IS EITHER NOT
DEFINED OR IS NOT USABLE

On another DB2 9 and V8 system it


precompiled, compiled etc. but the
run failed with
THE LENGTH OF INPUT HOST
VARIABLE NUMBER 17 IS
NEGATIVE OR GREATER THAN
THE MAXIMUM

But Why?

13 Multi-Row 4/21/2008 © 2007 IBM Corporation


SWG BetaWorks

Why where there problems?


ƒ PMR 24142,180,000 Has The Answer
– It refers to Application Programming and SQL Guide 2.4.3.6
Declaring host variable arrays
– 2.4.3.6 could not be found but a search on Declaring host variable
arrays in the PDF found:-

Example: The following example shows declarations of a fixed-length


character array and a varying-length character array:
01 OUTPUT-VARS.
05 NAME OCCURS 10 TIMES.
49 NAME-LEN PIC S9(4) COMP SYNC.
49 NAME-DATA PIC X(40).
05 SERIAL-NUMBER PIC S9(9) COMP-4 OCCURS 10 TIMES.
14 Multi-Row 4/21/2008 © 2007 IBM Corporation
SWG BetaWorks

Varchar Definition

ƒ The Varchar host variable is defined as


05 NAME OCCURS 10 TIMES.
49 NAME-LEN PIC S9(4) COMP SYNC.
49 NAME-DATA PIC X(40).

The difference between the example and the DCLGEN is


the word SYNC

15 Multi-Row 4/21/2008 © 2007 IBM Corporation


SWG BetaWorks

What does SYNC do

ƒ The COBOL manual says


The SYNCHRONIZED clause is
never required, but can
improve performance on some
systems for binary items used
in arithmetic.

For S9(4) COMP it aligns on a


half word boundary i.e. 2bytes
For S9(9) COMP it aligns on a full
word boundary i.e.4 bytes

16 Multi-Row 4/21/2008 © 2007 IBM Corporation


SWG BetaWorks

Edit Macro – OCC to Add Multi-Row to A DCLGEN


ƒ /* REXX*/
ƒ "ISREDIT MACRO (NUMOCC) NOPROCESS" NUMOCC is No. Of Rows
ƒ "ISPEXEC CONTROL ERRORS RETURN"
ƒ IF NUMOCC = '' THEN NUMOCC = 100 If no parm set it to 100

ƒ "ISREDIT SEEK ' 10 '" Lookfor


Look forthe
thefirst
first10
10level
level

ƒ RCD = RC Store return code

ƒ CHTO = "' OCCURS "NUMOCC".'" Build change to string

ƒ DO WHILE RCD = 0 Loop while return code is zero

ƒ "ISREDIT CHANGE '.' "CHTO Issue change command

ƒ "ISREDIT SEEK ' 10 '" Look for next 10 level

ƒ RCD = RC Return code 4 is not found 0 is found


ƒ END
ƒ "ISREDIT CHANGE ALL 'COMP.' 'COMP SYNC.‘” Finally change all comp
To comp sync
17 Multi-Row 4/21/2008 © 2007 IBM Corporation
SWG BetaWorks

New DCLGEN for Multi-Row after running OCC


ƒ 01 W2-TPART.
ƒ * *************************************************************
ƒ * PARTITION
ƒ 10 W2-PARTITION PIC S9(4) USAGE COMP OCCURS 100.
ƒ * *************************************************************
ƒ 10 W2-TSNAME OCCURS 100.
ƒ * TSNAME LENGTH
ƒ
ƒ *
49 W2-TSNAME-LEN
TSNAME
PIC S9(4) USAGE COMP SYNC.
Each 10 Level has an occurs
ƒ 49 W2-TSNAME-TEXT PIC X(24).
ƒ * *************************************************************
ƒ 10 W2-DBNAME OCCURS 100.
ƒ * DBNAME LENGTH
ƒ 49 W2-DBNAME-LEN PIC S9(4) USAGE COMP SYNC.
ƒ * DBNAME
ƒ 49 W2-DBNAME-TEXT PIC X(24).
ƒ * *************************************************************
ƒ
ƒ *
10 W2-IXNAME OCCURS 100.
IXNAME LENGTH
Each Length field has SYNC
ƒ 49 W2-IXNAME-LEN PIC S9(4) USAGE COMP SYNC.
ƒ * IXNAME
ƒ 49 W2-IXNAME-TEXT PIC X(128).
ƒ * *************************************************************
ƒ 10 W2-IXCREATOR OCCURS 100.
ƒ * IXCREATOR LENGTH
ƒ 49 W2-IXCREATOR-LEN PIC S9(4) USAGE COMP SYNC.
ƒ * IXCREATOR
ƒ 49 W2-IXCREATOR-TEXT
ƒ PIC X(128).

18 Multi-Row 4/21/2008 © 2007 IBM Corporation


SWG BetaWorks

Multi-Row Insert Code – Build the 100 occurrences


READ FD-FB-FILE
AT END MOVE 'Y' TO W-END-OF-FILE
Read the first record
END-READ.
PERFORM B000-PROCESS-RECORDS UNTIL END-OF-FILE.
Zeroise subscript
B000-PROCESS-RECORDS SECTION.
MOVE 0 TO W-NUM-ROWS. Loop for 100 records
PERFORM VARYING W-NUM-ROWS FROM 1 BY 1
UNTIL W-NUM-ROWS > 100 Or end of file reached
OR END-OF-FILE
MOVE FB-REC TO W-TPART
Move record to 1st DCLGEN
MOVE W-PARTITION TO W2-PARTITION (W-NUM-ROWS)
MOVE W-TSNAME TO W2-TSNAME (W-NUM-ROWS)
MOVE W-DBNAME TO W2-DBNAME (W-NUM-ROWS)
Move 1st DCLGEN to
MOVE W-IXNAME TO W2-IXNAME (W-NUM-ROWS)
MOVE W-IXCREATOR TO W2-IXCREATOR (W-NUM-ROWS) Next occurrence of
…….
MOVE W-RELCREATED TO W2-RELCREATED (W-NUM-ROWS) 2nd DCLGEN
READ FD-FB-FILE
AT END MOVE 'Y' TO W-END-OF-FILE
END-READ Read next record
END-PERFORM.

19 Multi-Row 4/21/2008 © 2007 IBM Corporation


SWG BetaWorks

Multi-Row INSERT Code – Insert the 100 rows


IF W-NUM-ROWS > 0 Are there rows to insert
SUBTRACT 1 FROM W-NUM-ROWS
EXEC SQL
INSERT INTO SYSTABLEPART
VALUES
(:W2-PARTITION ,
:W2-TSNAME ,
:W2-DBNAME ,
The host variables are the
:W2-IXNAME ,
:W2-IXCREATOR ,
Names of the fields with the
:W2-PQTY ,
……..
Occurs clause on but no
:W2-FORMAT ,
:W2-REORG-LR-TS ,
Subscript is needed
:W2-RELCREATED )
FOR :W-NUM-ROWS ROWS
NOT ATOMIC CONTINUE ON SQLEXCEPTION Multi-row parameters
END-EXEC

20 Multi-Row 4/21/2008 © 2007 IBM Corporation


SWG BetaWorks

FOR :W-NUM-ROWS ROWS

ƒ FOR 100 ROWS could have been coded


– What if the file contained 226 records
– First INSERT would insert 100 rows
– Second INSERT would insert 100 rows
– Third INSERT would insert 100 rows
– So we now have 300 rows but only 226 records
The last 74 would be the same as the last 74 of the second
insert but using FOR :W-NUM-ROWS ROWS tells DB2
that the last Insert has 26 so no unexpected duplicates

21 Multi-Row 4/21/2008 © 2007 IBM Corporation


SWG BetaWorks

NOT ATOMIC CONTINUE ON SQLEXCEPTION


ƒ ATOMIC Specifies that if the insert for any row fails,
all changes made to the database by any of the
inserts, including changes made by successful inserts,
are undone. This is the default.

ƒ NOT ATOMIC CONTINUE ON SQLEXCEPTION


Specifies that, regardless of the failure of any
particular insert of a row, the INSERT statement will
not undo any changes made to the database by the
successful inserts of other rows, and inserting will be
attempted for subsequent rows.

22 Multi-Row 4/21/2008 © 2007 IBM Corporation


SWG BetaWorks

How to handle Errors in Single Row Insert Program

ƒ Add the following in B000 to cause an error

IF W-ROW-COUNT = 10
MOVE -10 TO W-DBNAME-LEN
END-IF.

ƒ This will stop the 11th record from being inserted

23 Multi-Row 4/21/2008 © 2007 IBM Corporation


SWG BetaWorks

Using DSNTIAR to Obtain the Message


Working Storage
ƒ
01 W150-ERROR-MESSAGE.
03 W150-ERR-LEN PIC S9(4) COMP VALUE +288. Total Length of following 4 fields
03 W150-ERR-MSG-1 PIC X(72).
03 W150-ERR-MSG-2 PIC X(72).
03 W150-ERR-MSG-3 PIC X(72).
03 W150-ERR-MSG-4 PIC X(72).
01 W150-ERROR-MESSAGE-LEN PIC S9(9) COMP VALUE +72. Length of each message line

Procedure Division

CALL 'DSNTIAR' USING SQLCA


W150-ERROR-MESSAGE
W150-ERROR-MESSAGE-LEN.

DISPLAY W150-ERR-MSG-1.
DISPLAY W150-ERR-MSG-2.
DISPLAY W150-ERR-MSG-3.
DISPLAY W150-ERR-MSG-4.
MOVE 12 TO RETURN-CODE.
GOBACK.

24 Multi-Row 4/21/2008 © 2007 IBM Corporation


SWG BetaWorks

Output From Job after Error


DSNT408I SQLCODE = -311, ERROR: THE LENGTH OF INPUT HOST VARIABLE
NUMBER 3 IS NEGATIVE OR GREATER THAN THE MAXIMUM
DSNT418I SQLSTATE = 22501 SQLSTATE RETURN CODE
DSNT415I SQLERRP = DSNXRIHB SQL PROCEDURE DETECTING ERROR

This has been the recommended method


Of handling errors in programs using DB2

25 Multi-Row 4/21/2008 © 2007 IBM Corporation


SWG BetaWorks

Put Same Error Into Multi-Row Insert

ƒ Add the following in B000 to cause an error

MOVE -10 TO W2-DBNAME-LEN (11)

ƒ This will stop the 11th record from being inserted

26 Multi-Row 4/21/2008 © 2007 IBM Corporation


SWG BetaWorks

Job Output After Error in Multi-Row Insert


DSNT408I SQLCODE = -253, ERROR: A NON-ATOMIC INSERT STATEMENT
SUCCESSFULLY COMPLETED FOR SOME OF THE REQUESTED ROWS,
POSSIBLY WITH WARNINGS, AND ONE OR MORE ERRORS

DSNT418I SQLSTATE = 22529 SQLSTATE RETURN CODE

This is not very helpful as it only tells us that some worked


SQLERRD (3) can be used to find the number inserted

27 Multi-Row 4/21/2008 © 2007 IBM Corporation


SWG BetaWorks

What Happens if the INSERT is Atomic

DSNT408I SQLCODE = -311, ERROR: THE LENGTH OF INPUT HOST


VARIABLE
NUMBER 3 IS NEGATIVE OR GREATER THAN THE MAXIMUM
DSNT418I SQLSTATE = 22501 SQLSTATE RETURN CODE
DSNT415I SQLERRP = DSNXRIHB SQL PROCEDURE DETECTING ERROR

This message now tells what the real problem is but the 10 Inserted rows have
been rolled back.

How can correct messages be issued and allow inserts to continue.

28 Multi-Row 4/21/2008 © 2007 IBM Corporation


SWG BetaWorks

How to issue correct messages and allow inserts

ƒ NOT ATOMIC CONTINUE ON SQLEXCEPTION will


allow processing to continue
ƒ But the correct error can not be seen by calling
DSNTIAR

ƒ The solution is to use GET DIAGNOSTICS

29 Multi-Row 4/21/2008 © 2007 IBM Corporation


SWG BetaWorks

Coding GET DIAGNOSTICS


ƒ Working Storage
03 W-DIAG-SUB PIC S9(9) COMP.
03 W-DB2-RETURNED-SQLCODE PIC S9(9) COMP VALUE 0.
These fields must be defined
03 W-DB2-RETURNED-SQLSTATE PIC X(5).
03 W-DB2-ROW-NUMBER PIC S9(31) COMP-3. Correctly or
03 W-DIAG-ERRORS PIC S9(9) COMP.
01 W600-DIAG-AREA.
10 W600-DIAGNOSTICS. GET DIAGNOSTICS
49 W600-DIAGLEN PIC S9(4) COMP VALUE 0.
49 W600-DIAG PIC X(32672).
Will not work
ƒ Procedure Division
EXEC SQL
GET DIAGNOSTICS :W-DIAG-ERRORS = NUMBER This tells us how many errors have been found
END-EXEC
IF W-DIAG-ERRORS > 0
PERFORM VARYING W-DIAG-SUB FROM 1 BY 1
UNTIL W-DIAG-SUB > W-DIAG-ERRORS Loop round so we get all errors
EXEC SQL
GET DIAGNOSTICS CONDITION :W-DIAG-SUB
Get next error
:W-DB2-RETURNED-SQLCODE = DB2_RETURNED_SQLCODE , Get SQLCODE
:W-DB2-RETURNED-SQLSTATE = RETURNED_SQLSTATE ,
:W600-DIAGNOSTICS = MESSAGE_TEXT ,
Get SQLSTATE
:W-DB2-ROW-NUMBER = DB2_ROW_NUMBER
Get Message
END-EXEC
Row number which caused this error

30 Multi-Row 4/21/2008 © 2007 IBM Corporation


SWG BetaWorks

Results of DISPLAYING the Errors


ƒ SQLCODE -0311
ƒ W-NUM-ROWS 100
ƒ W-DIAG-ERRORS
ƒ 0000000001
ƒ W-DB2-RETURNED-SQLCODE
ƒ 000000031J
ƒ W-DB2-RETURNED-SQLSTATE
ƒ 22501
ƒ W-DB2-ROW-NUMBER
ƒ 0000000000000000000000000000011
ƒ THE LENGTH OF INPUT HOST VARIABLE NUMBER 3 IS
NEGATIVE OR GREATER THAN THE MAXIMUM

31 Multi-Row 4/21/2008 © 2007 IBM Corporation


SWG BetaWorks

Multi-row FETCH
ƒReturns multiple rows on one API crossing
ƒ"wide" cursor with locks on multiple rows
ƒSupports scrollable and non-scrollable, static and
dynamic SQL
ƒSignificant performance boost
ƒDSNTEP4 = DSNTEP2 + MRF
DECLARE C1 CURSOR
WITH ROWSET POSITIONING
FOR SELECT COL1, COL2 FROM T1;
OPEN C1;
FETCH FROM C1
FOR :hv ROWS INTO :ARRAY1, :ARRAY2;

32 Multi-Row 4/21/2008 © 2007 IBM Corporation


SWG BetaWorks

Single Row Fetch Example – PLFSR01


Working Storage
05 W-PARTITION PIC S9(4) COMP.
05 W-TSNAME.
Host Variables have been
49 W-TSNAME-LEN PIC S9(4) COMP.
49 W-TSNAME-TEXT PIC X(24). Hard coded rather than using
05 W-DBNAME.
49 W-DBNAME-LEN PIC S9(4) COMP. DCLGEN Fields as
49 W-DBNAME-TEXT PIC X(24).
05 W-IXNAME.
49 W-IXNAME-LEN PIC S9(4) COMP.
Different installations have
49 W-IXNAME-TEXT PIC X(128).
05 W-IXCREATOR. Different standard
49 W-IXCREATOR-LEN PIC S9(4) COMP.
49 W-IXCREATOR-TEXT PIC X(128).

Procedure Division
EXEC SQL
DECLARE C1 SCROLL CURSOR FOR
SELECT PARTITION, TSNAME, DBNAME, IXNAME, IXCREATOR
Declare the cursor using single row
FROM SYSTABLEPART
END-EXEC.
EXEC SQL
OPEN C1 Open the cursor
END-EXEC.
PERFORM B000-FETCH
UNTIL W-SQLCODE NOT = 0.
ƒ B000-FETCH SECTION.
ƒ EXEC SQL
ƒ
ƒ
FETCH FROM C1
INTO :W-PARTITION ,
Fetch one row at a time
ƒ :W-TSNAME ,
ƒ :W-DBNAME ,
ƒ :W-IXNAME ,
ƒ :W-IXCREATOR
ƒ END-EXEC.

33 Multi-Row 4/21/2008 © 2007 IBM Corporation


SWG BetaWorks

JCL to run PLFSRI01


//PLFSRI01 EXEC PGM=IKJEFT01,DYNAMNBR=25,ACCT=SHORT,
// REGION=4096K
//STEPLIB DD DSN=SYS2.DB2.V910.SDSNLOAD,DISP=SHR
// DD DSN=SYS2.DB2.V910.SDSNEXIT.PIC,DISP=SHR
// DD DSN=FLETCHP.MASTER.LOAD,DISP=SHR
//SYSUDUMP DD SYSOUT=*
//SYSTSPRT DD SYSOUT=*
//SYSOUT DD SYSOUT=*
//SYSABOUT DD SYSOUT=*
//SYSTSIN DD *
DSN SYSTEM(PB1I)
RUN PROGRAM(PLFSRF01) PLAN(PLFSRF01)
END
/*

34 Multi-Row 4/21/2008 © 2007 IBM Corporation


SWG BetaWorks

Multi-Row Fetch example – PLFMRI01


Working Storage
05 W-PARTITION PIC S9(4) COMP OCCURS 100 TIMES.
05 W-TSNAME OCCURS 100 TIMES.
49 W-TSNAME-LEN PIC S9(4) COMP SYNC.
49 W-TSNAME-TEXT PIC X(24).
05 W-DBNAME OCCURS 100 TIMES.
49 W-DBNAME-LEN PIC S9(4) COMP SYNC.
49 W-DBNAME-TEXT PIC X(24).
05 W-IXNAME OCCURS 100 TIMES. SYNC is required for length fields
49 W-IXNAME-LEN PIC S9(4) COMP SYNC.
49 W-IXNAME-TEXT PIC X(128).
05 W-IXCREATOR OCCURS 100 TIMES.
49 W-IXCREATOR-LEN PIC S9(4) COMP SYNC.
49 W-IXCREATOR-TEXT PIC X(128).
Procedure Division
EXEC SQL
DECLARE C1 SCROLL CURSOR WITH ROWSET POSITIONING FOR WITH ROWSET POSTIONING is
SELECT PARTITION, TSNAME, DBNAME, IXNAME, IXCREATOR
FROM SYSTABLEPART
END-EXEC.
Required for multi-row
EXEC SQL
OPEN C1
END-EXEC.
PERFORM B000-FETCH
UNTIL W-SQLCODE NOT = 0.
ƒ B000-FETCH SECTION.
ƒ EXEC SQL
ƒ FETCH NEXT ROWSET FROM C1 FOR 100 ROWS Tell DB2 how many rows to FETCH
ƒ INTO :W-PARTITION ,
ƒ :W-TSNAME ,
ƒ :W-DBNAME ,
ƒ :W-IXNAME ,
ƒ :W-IXCREATOR
ƒ END-EXEC.

35 Multi-Row 4/21/2008 © 2007 IBM Corporation


SWG BetaWorks

Processing the rows returned by Multi-Row fetch

PERFORM VARYING W-SUB FROM 1 BY 1


UNTIL W-SUB > 100
DISPLAY W-PARTITION (W-SUB) ' '
W-TSNAME-TEXT (W-SUB) (1:W-TSNAME-LEN (W-SUB)) ' '
W-DBNAME-TEXT (W-SUB) (1:W-DBNAME-LEN (W-SUB)) '‘,
W-IXNAME-TEXT (W-SUB) (1:W-IXNAME-LEN (W-SUB)) ' '
W-IXCREATOR-TEXT (W-SUB) (1:W-IXCREATOR-LEN (W-
SUB))
END-PERFORM

Loop round in this case Displaying 100 rows returned

36 Multi-Row 4/21/2008 © 2007 IBM Corporation


SWG BetaWorks

Processing Rows Returned

ƒ If the table has 226 rows


ƒ First Fetch returns 100 – SQLCODE 0
ƒ Second Fetch returns 100 – SQLCODE 0
ƒ Third fetch can only return 26 – SQLCODE 100

ƒ How can the program tell that 26 rows were


returned on the last call?

37 Multi-Row 4/21/2008 © 2007 IBM Corporation


SWG BetaWorks

How to tell How many rows have been returned


ƒ SQLERRD (3) in SQLCA

ƒ Or Use Get DIAGNOSTICS


EXEC SQL
GET DIAGNOSTICS
:W-DIAG-ROW-COUNT = ROW_COUNT
END-EXEC

38 Multi-Row 4/21/2008 © 2007 IBM Corporation


SWG BetaWorks

Processing last Rowset

PERFORM VARYING W-SUB FROM 1 BY 1


UNTIL W-SUB > W-DIAG-ROW-COUNT
DISPLAY W-PARTITION (W-SUB) ' '
W-TSNAME-TEXT (W-SUB) (1:W-TSNAME-LEN (W-SUB)) ' '
W-DBNAME-TEXT (W-SUB) (1:W-DBNAME-LEN (W-SUB)) '‘,
W-IXNAME-TEXT (W-SUB) (1:W-IXNAME-LEN (W-SUB)) ' '
W-IXCREATOR-TEXT (W-SUB) (1:W-IXCREATOR-LEN (W-SUB))
END-PERFORM.

39 Multi-Row 4/21/2008 © 2007 IBM Corporation


SWG BetaWorks

Any Questions
ƒ Please contact
ƒ Paul Fletcher
ƒ FLETCHPL@UK.IBM.COM

40 Multi-Row 4/21/2008 © 2007 IBM Corporation

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy