简体   繁体   中英

db2 rowset cursor parametrization

Would there be a way to program a parameter that is not hard coded to this?

In place of :SomeValue host variable in this question/snippet:

    EXEC SQL                                                      

     FETCH NEXT ROWSET FROM C_NJSRD2_cursor_declared_and_opened                        
     FOR :SomeValue ROWS                                            
     INTO                                                   

             :NJCT0022.SL_ISO2         :NJMT0022.iSL_ISO2   
     etc....

Here is some clarification:

Parametrization of the request like posted in opening question actually works in case I set the host variable :SomeValue to 1 and define host variable arrays for filling from database to size 1 like

  struct
  ??<
       char       SL_ISO2   ??(1??) ??(3??); // sorry for Z/os trigraphs
       etc..

And it also works if I set the host variable arrays to a larger defined integer value (ie 20) and hard code the value (:SomeValue) to that value in cursor rowset fetch.

 EXEC SQL

     FETCH NEXT ROWSET FROM C_NJSRD2
     FOR 20 ROWS
     INTO

             :NJCT0022.SL_ISO2         :NJMT0022.iSL_ISO2
            ,:NJCT0022.BZ_COUNTRY      :NJMT0022.iBZ_COUNTRY
            ,:NJCT0022.KZ_RISK         :NJMT0022.iKZ_RISK

I wish to receive the number of rows from the calling program (COBOL), a and ideally set the size of host variable arrays accordingly. To avoid variable array sizing problem, oversizing host variable arrays to a larger value would be good also.

Those combinations return compile errors: HOST VARIABLE ARRAY "NJCT0022" IS EITHER NOT DEFINED OR IS NOT USABLE

And in good tradition here is an answer to my own question. The good people of SO will upvote me for sure now.

Rowsets are very fast and beside making host variable arrays arrays or for chars array arrays these cursors just require adjusting program functions for saving values and setting null values in loops. They are declared like this:

     FETCH NEXT ROWSET FROM C_NJSRD2
     FOR 19 ROWS

Rowset cursors can not change host array (that is array array) size dynamically. Unlike scroll cursors they can not jump to position or go backwards.They can however go forward not by the whole preset rowset number of rows but just by a single row.

     FETCH NEXT ROWSET FROM C_NJSRD2 FOR 1 ROWS
     INTO

So to answer my question, to make the algorithm able to accept any kind row number requested for fetches it is basically just a question of segmenting the request to rowsets and eventually one line fetches until the requested number is met. To calculate loop counters for rowsets and single liners:

if((iRowCount>iRowsetPreset)&&
 ((iRowCount%iRowsetPreset)!=0))
 ??<
  iOneLinersCount = iRowCount % iRowsetPreset;
  iRowsetsCount = (iRowCount - iOneLinersCount)
    / iRowsetPreset;    
 ??>
if ((iRowCount==iRowsetPreset) _OR_
  ((iRowCount%iRowsetPreset)==0))
  ??<
    iOneLinersCount = 0;
    iRowsetsCount = iRowCount / iRowsetPreset;
  ??>
if (iRowCount<iRowsetPreset)
  ??<
    iOneLinersCount = iRowCount;
    iRowsetsCount = 0;
  ??>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM