Multi-row FETCH requires a proper understanding of how to set up the environment in terms of declaring the cursor, setting up the host variable arrays, and performing the FETCH operations. Using multi-row FETCH not only has an impact on how you handle FETCH operations in your application, but also has repercussions on how the positioned UPDATE and positioned DELETE statements are handled. We discuss the details in the following visuals.
Converting To Anthony Tichonoff Florida Hospital MIS January 2007 With Ease Multi-Row Fetch
A group of rows for the result table of a query that are returned by a single fetch statement.
The maximum size of the rowset is 32767
The program fetch call controls how many rows are returned in a rowset.
It is possible to intertwine single-row and multi-row fetches within the same program.
Single Fetch Single Row Multiple Fetch Multiple Rows
Declare Syntax Changes rowset-positioning: Default is WITHOUT ROWSET POSITIONING DECLARE CURSOR Syntax NO SCROLL DECLARE cursor-name ASENSITIVE INSENSITIVE SENSITIVE STATIC DYNAMIC SCROLL CURSOR FOR Select-Statement select-name rowset-positioning WITH HOLD WITH RETURN WITHOUT ROWSET POSITIONING WITH ROWSET POSITIONING
Declare C1 as the cursor of a query to retrieve a rowset from table PayRoll
Declare C1 Cursor
With Rowset Positioning For
With Rowset Positioning specifies whether multiple rows of data can be accessed as a rowset on a single Fetch statement
Fetch Syntax Changes fetch-orientation: multiple-row-fetch: rowset-positioned: , FETCH Syntax FETCH INSENSITIVE SENSITIVE fetch-orientation FROM cursor-name single-row-fetch multiple-row fetch rowset-positioned BEFORE AFTER row-positioned NEXT ROWSET PRIOR ROWSET FIRST ROWSET LAST ROWSET CURRENT ROWSET ROWSET STARTING AT ABSOLUTE RELATIVE host-variable integer-constant FOR host-variable integer-constant ROWS INTO INTO DESCRIPTOR host-variable-array descriptor-name
Fetching into arrays will return multiple rows in a single API call.
PayRoll Row 1 Multiple Fetch Fetching Multiple Rows Row 2 Row 3 Row 4 HA 1 HA 2 HA 3 HA 4 HA 5 HA 6 Each host variable array can only point to a single DB2 column Arrays may not be arrays of structures
Fetch Next Rowset From CS1 For :row-limt Rows Into :HVA-1 ,:HVA-2 ,:HVA-3 If (SQLCode = 0 Or SQLCode = 100) And SQLERRD(3) > 0 - Process SQLERRD(3) rows Else If SQLCode = 100 and SQLERRD(3) = 0 - Close Cursor Else Handle SQL Error
How to bridge into multi-row fetching with ease. Converting To Multi-row Fetching With Ease
Fetch SQL Statement (Changes made to acquire multi-row fetch) Add Pre-Fetch Code (Handles decision where to get next row) Add Post-Fetch Code (Handles RCs and array positioning) Add Variable Assignments (Move :HVA elements into :HV)
Coding the Working Storage Table Working Storage Cursor Working Storage Don't forget to code your Host Variable Arrays
Coding the Prefetch Check array boundaries Get Next Array Row At the end reset SQL return code Fetch next rowset