× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



Just a point of reference. *AUTO is a V7R4 feature. Do not know what
release level you are at.

I have been doing dynamic arrays and data structures for 20 years. Very
simple.

In your case, I assume inside a procedure.

This example makes no sense since you could just do a count with the SQL
statement but just as an example of a dynamic array.

dcl-c SQL_STATE_NO_ROW '02000';
dcl-c SQL_STATE_OK '00000';
dcl-c TRUE '1';

dcl-c INITIAL_ELEMENTS 5;
dcl-c ADDITIONAL_ELEMENTS 10;

dcl-s NumberAllocated Int(10);
dcl-s CurrentIndex Int(10);
dcl-s NumberOfBears Int(10);
dcl-s TotalWeight Int(10);

dcl-ds dsAnimals Qualified Dim(10000) Based(PtrAnimals);
Type Char(10);
Weight Int(10);
end-ds;

Exec SQL Declare AnimalsCursor Cursor For
Select Type,
Weight
From ANIMALS
Where Type = 'BEARS';

Exec SQL Open AnimalsCursor;
If SqlState <> SQL_STATE_OK;
// Throw error.
EndIf;

DoW TRUE;
Exec Sql Fetch
From AnimalsCursor
Into :dsAnimals;
Select;
When SqlState = SQL_STATE_NO_ROW;
Exec Sql Close AnimalsCursor;
Leave;
When SqlState = SQL_STATE_OK;
CurrentIndex += 1;
If CurrentIndex > NumberAllocated;
If NumberAllocated = 0;
NumberAllocated = INITIAL_ELEMENTS;
PtrAnimals = %Alloc(NumberAllocated * %Size(dsAnimals));
Else;
NumberAllocated += ADDITIONAL_ELEMENTS;
PtrAnimals = %ReAlloc(PtrAnimals:NumberAllocated *
%Size(dsAnimals));
EndIf;
EndIf;
NumberOfBears += 1;
TotalWeight += dsAnimals.Weight;
Other;
// Throw error.
EndSl;
EndDo;

Dsply NumberOfBears;
Dsply TotalWeight;

// Important. Always deallocate memory.
DeAlloc PtrAnimals;


On Thu, Feb 8, 2024 at 4:43 PM Jon Paris <jon.paris@xxxxxxxxxxxxxx> wrote:

The answer to the question as asked is yes. But if you create the array
as Dim(*Auto: .... ) you don't need to set it.

Here's an example:

Dcl-Ds results ExtName('ANIMALS') qualified Dim( *Auto : 1000 );
End-Ds;

Dcl-Ds animal likeDs(results);

Dcl-s type like(results.type);

Exec SQL
Declare animalsCursor cursor for
Select * from ANIMALS where type = :type;

Exec SQL
Open animalsCursor;

Exec SQL
Fetch from animalsCursor for 1000 rows into :results;

Dsply ( 'Found ' + %Char( %Elem( results ) ) + ' matching animals' );

For-Each animal in results;
Dsply ( 'Id: ' + %Char(animal.Id) + ' - Name: ' + animal.name );
EndFor;

Exec SQL
Close animalsCursor;

In fact you don't even need to reset the element count before the next
select - the SQL preprocessor seems to do that for you.


Jon P.


On Feb 8, 2024, at 7:35 PM, Thomas Burrows <
thomas.burrows.1957@xxxxxxxxx> wrote:

Is it possible to dynamically set the number of OCCURRENCES "DIM(##)" on
a
dynamically created Data Structure?

dcl-ds EmpFile_ds extname('EMPPF')
qualified dim(10) ;

What I am wanting to do is load the contents of a SQL CURSOR which could
be
1 to N something. Taking the number of rows in the CURSOR to dynamically
create the Data Structure.

Thomas
mobile 469 693 2533
--
This is the RPG programming on IBM i (RPG400-L) mailing list
To post a message email: RPG400-L@xxxxxxxxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: https://lists.midrange.com/mailman/listinfo/rpg400-l
or email: RPG400-L-request@xxxxxxxxxxxxxxxxxx
Before posting, please take a moment to review the archives
at https://archive.midrange.com/rpg400-l.

Please contact support@xxxxxxxxxxxxxxxxxxxx for any subscription
related questions.


--
This is the RPG programming on IBM i (RPG400-L) mailing list
To post a message email: RPG400-L@xxxxxxxxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: https://lists.midrange.com/mailman/listinfo/rpg400-l
or email: RPG400-L-request@xxxxxxxxxxxxxxxxxx
Before posting, please take a moment to review the archives
at https://archive.midrange.com/rpg400-l.

Please contact support@xxxxxxxxxxxxxxxxxxxx for any subscription related
questions.



As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

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.