|
What am I doing wrong in the code below? Each subsequent fetch from the cursor seems to load into element 1 of the array.
(There are 42,654 rows to be loaded. Granted, this is an unusually large number to load, but I'm accessing it by index to build random test data and loading rows one at a time works great, so I'm not stuck. But I am curious about this and why I can't get this to work.)
Incidentally, snd-msg is a great replacement for dsply in these situations--thanks Marco.
After a run of the code below, these are the results:
Allocated default: 100
Allocated after *alloc: 42755
"fetch for 'n' rows" : 42654
Elements before fetch: 0
After First Fetch. Elements: 42654. SQLSTATE: 00000. Allocated: 42755
After Loop Fetch. Elements: 0. SQLSTATE: 02000. Allocated: 42755
Final Elements: 0. Allocated: 42755
If I set rows to fetch to 25,000 these are the results:
Allocated default: 100
Allocated after *alloc: 42755
"fetch for 'n' rows" : 25000
Elements before fetch: 0
After First Fetch. Elements: 25000. SQLSTATE: 00000. Allocated: 42755
After Loop Fetch. Elements: 17654. SQLSTATE: 00000. Allocated: 42755
After Loop Fetch. Elements: 0. SQLSTATE: 02000. Allocated: 42755
Final Elements: 0. Allocated: 42755
This is the demo code:
**free
ctl-opt Option(*nounref: *nodebugio: *srcstmt);
dcl-c SQLSUCCESS '00000';
dcl-c SQLNOMOREDATA '02000';
dcl-ds csz_a dim(*auto : 50000) qualified;
zip int(10);
end-ds;
dcl-s cszCount int(10);
exec sql set option closqlcsr=*endmod;
exec sql declare csz_cur cursor for
select zip
from lennons1.csz where length(trim(city)) <= 20;
// Allocate enough memory/elements
exec sql select count(*) into :cszCount
from lennons1.csz where length(trim(city)) <= 20;
snd-msg ('Allocated default: ' + %char(%elem(csz_a : *alloc)));
%elem(csz_a : *alloc) = cszCount + 1;
snd-msg ('Allocated after *alloc: ' + %char(%elem(csz_a : *alloc)));
// cszCount = 25000; // pick a number, e.g. 25,000
snd-msg ('"fetch for ''n'' rows" : ' + %char(cszCount));
snd-msg ('Elements before fetch: ' + %char(%elem(csz_a)));
exec sql open csz_cur;
exec sql fetch from csz_cur for :cszCount rows into :csz_a;
snd-msg ('After First Fetch. Elements: ' + %char(%elem(csz_a))
+ '. SQLSTATE: ' +SQLSTATE+ '. Allocated: '
+%char(%elem(csz_a : *alloc)));
dow sqlstate <> SQLNOMOREDATA;
exec sql fetch next from csz_cur for :cszCount rows into :csz_a;
Snd-msg ('After Loop Fetch. Elements: ' + %char(%elem(csz_a))
+ '. SQLSTATE: ' +SQLSTATE
+ '. Allocated: ' + %char(%elem(csz_a : *alloc)));
enddo;
snd-msg ('Final Elements: ' + %char(%elem(csz_a)) + '. Allocated: '
+%char(%elem(csz_a : *alloc)));
return;
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.