Using variable content as literal in USE-INDEX

Posted by DimitriG4 on 14-Jun-2010 16:08

i've stumbled on  the following:

case v-order = 1

FOR EACH t-tlist USE-INDEX order-a

....

END.

.

case v-order = 2

FOR EACH t-tlist USE-INDEX order-b

....

END.

what I'm hoping for is being able to assign

"order-a"  to v-index-name in a case statement

and then just use one

FOR EACH t-list USE-INDEX v-index-name

is there some obvious or magic syntax I am missing ?

All Replies

Posted by Admin on 14-Jun-2010 16:14

is there some obvious or magic syntax I am missing ?

First, rather than using USE-INDEX consider using BY...

Then do something like this:

DEFINE QUERY qttlist for t-tlist.

CASE v-order:

WHEN 1 THEN

QUERY qttlist:QUERY-PREPARE ("FOR EACH t-list BY sortfieldA") .

WHEN 2 THEN

QUERY qttlist:QUERY-PREPARE ("FOR EACH t-list BY sortfieldB") .

END CASE.

QUERY qttlist:QUERY-OPEN ().

GET FIRST qttlist.

Posted by Thomas Mercer-Hursh on 14-Jun-2010 16:18

The simple direct answer is no.  You can tell such things by looking in the manual and it it provides no option for an expression, then you are going to have to use a literal.

Of course, by and large you shouldn't be using USE-INDEX.  Generally, it is much better to specify the sort order and let Progress pick the index.  This is a left over from earlier versions where ABL wasn't so clever about index selection.  On some queries, Progress can use more than one index to improve performance, a behavior which you block by specifying an index.

The real answer to your need (rather than your question) is to explore dynamic queries.  With a dynamic query you can build the field list and sort order that is right for the specific paramaters and then have a single standard loop to process the results of that query.

Posted by DimitriG4 on 14-Jun-2010 16:35

Dynamic queries are a lot of fun. I've changed a few I stumbled on to dynamic, instead of copying and pasting the same chunk of code "one more time".

This thread is closed