Problem solve Get help with specific problems with your technologies, process and projects.

Options for sorting subfiles in RPG

I sort subfiles in RPG LE by copying the subfile records to array and then SORTA and copy the array back to the subfile. Is there another ways to sort subfile in RPG? How can I pass (to QSORT) a pointer to the subfile?
First, the bad news: You cannot pass a pointer to the subfile. That pointer is not available to your program.

Now, some better news. If you are a Professional member of iSeries Network, you can read an article about using keyed data queues to sort subfiles in RPG in the November 2004 edition of the iSeries News magazine. The article, and a previous article on using keyed data queues to sort subfiles in COBOL, is also available at http://www.iseriesnetwork.com/ -- again, you must be a Professional member.

If you are not qualified to read the article, the gist of it is this:

1. Create a keyed data queue in QTEMP named for the program.

2. Load the subfile from your database.

3. If the user requests to see the data in a different order, clear the data queue and load it from the subfile. The key will be the key field chosen by the user. Note that the data length of a data queue entry must be exactly equal to the length of the subfile record you want to store (or whatever data structure you want to use), and the key length must be enough to hold the longest key value combination.

4. Once the data queue has been loaded, clear the subfile and reload it by reading from the data queue. The data is presented automatically in the keyed order.

There are some tricks involved, primarily in the handling of signed numbers as keys, which I am sure I can leave to you to fathom.

The APIs needed are QCLRDTAQ, QSNDDTAQ and QRCVDTAQ. You also need to use the CRTDTAQ command, either from a controlling CL program or from within the RPG program using QCMDEXC (ignoring error messages when the queue already exists).

Now, I have some reservations on sorting subfiles, because the longer the data is on the screen, the less current it is. With live data in my company, users need to know the up-to-date values - and if they refresh the screen, they believe that that's what they are seeing. If the program is only sorting the subfile, then the information is not up to date.

So use the same technique to copy data read from the database into the data queue, instead of the subfile data, and load the subfile from that. This will catch new, changed and deleted data and should not add much additional overhead to the process.


The Best Web Links: tips, tutorials and more.

Visit the ITKnowledge Exchange and get answers to your developing questions fast.

Ask the Experts yourself: Our application development gurus are waiting to answer your programming questions.

Dig Deeper on RPG iSeries programming