Question Regarding For First

Hello Everyone,

I have few basic confusions regarding FOR FIRST:

1. Why, FOR FIRST comes with block because it retrieves only one record into record buffer.Is there any other logic behind this?

Code:
FOR FIRST customer.
      DISPLAY name.
END.

2. As FOR FIRST use multiple index and FIND use single then why don't we use FOR FIRST instead of using FIND, apparently performance of FOR FIRST should be larger then FIND because of multiple indexing?

Thanks & Regards!
Rajat.
 
Last edited:
FOR FIRST is a FOR block with the same general implications on scoping and so forth as FOR EACH. For example, it causes a new frame to be generated. Contrast this:

disp "X".
find first order.
disp order.name.

... with this ...

disp "X".
for first order.
disp order name.

Of course these days we don't worry about frames too much as the code with the FIND is rarely anywhere near the code with the DISPLAY, but that's another matter.

I can't comment on multiple indices; we almost never use them and I wasn't aware that FIND didn't either.
 
1. Why, FOR FIRST comes with block because it retrieves only one record into record buffer.Is there any other logic behind this?

FOR FIRST is not a statement with block properties. FOR is a statement with block properties. EACH, FIRST, and LAST are optional keywords for the FOR statement.

2. As FOR FIRST use multiple index and FIND use single then why don't we use FOR FIRST instead of using FIND, apparently performance of FOR FIRST should be larger then FIND because of multiple indexing?

The fact that the compiler can in some circumstances use multiple indexes to resolve a query doesn't necessarily mean that it always will. It is important to understand the compiler's index selection rules. These are in both the documentation and the knowledge base. Also, multiple indexes doesn't necessarily provide better bracketing for a given query than would a single index.

Finally, it is important to know what the compiler means by he "FIRST" record in the table, and whether that matches your idea of what the first record is. They may not be the same. From the docs:
The AVM finds the first record before any sorting.
 
Back
Top