• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Difference Between FIND FIRST and FOR FIRST

#1
Hi Everybody,
Can anyone let me know the exact difference between FIND FIRST and FOR FIRST and where exactly it should be used?

Thanks in advance. :rolling:
 
#2
You should rarely, if ever, use either of these. Both of them are misleading and dangerous.

FOR FIRST is especially nasty -- it finds the record before any specified sorting using whatever index the index selection rules result in. A BY clause will be ignored.

If you are dealing with an ordered set of data use OPEN QUERY or FOR EACH with an appropriate BY clause.

If you are dealing with a unique record use FIND without the FIRST but with an appropriately selective WHERE clause that is supported by an index.
 
#3
Hi,
I am worried about the application that i am working on. I could find only FIND FIRST in most of the places. :(

...Ya i could get you. Thanks for ur valuable information and quick reply. :yippee:
 
#4
I do not completely agree with Tom. The by clause is not ignored. Take for instance this simple code example.

Code:
DEFINE TEMP-TABLE ttTest
    FIELD Nummer        AS INTEGER
    FIELD Naam          AS CHARACTER
    INDEX Nummer    IS PRIMARY Nummer
    INDEX Naam      Naam.
CREATE ttTest.
ASSIGN nummer = 1
       naam   = "E".
CREATE ttTest.
ASSIGN nummer = 2
       naam   = "D".
CREATE ttTest.
ASSIGN nummer = 3
       naam   = "C".
CREATE ttTest.
ASSIGN nummer = 4
       naam   = "B".
CREATE ttTest.
ASSIGN nummer = 5
       naam   = "A".
FOR FIRST ttTest BY naam:
    DISP tttest.
END.
FOR FIRST ttTest BY nummer:
    DISP ttTest.
END.
The main difference however is that the FIND statement is not able to combine multiple indexes. The FOR statement can. When using the for statement you do not have to test if a record is available and if you want to test if a record is not available it is more efficient to use can-find instead of find.
 

Casper

ProgressTalk.com Moderator
Staff member
#5
I do not completely agree with Tom. The by clause is not ignored. Take for instance this simple code example.
Well, if the field 'naam' wasn't indexed then the by clause /would/ be ingnored.... So pretty tricky.

Casper.
 
#6
Good catch.

You got me on a "technicality". My point was that the BY is ignored for sorting -- FOR FIRST doesn't sort its result set, it only returns the 1st entry in index order, even if that conflicts with any BY that happens to be specified. You got the expected answer because the naam index was used. And that happened because index selection rules use the BY as a tie-breaker.
 
#7
Hi,
I am worried about the application that i am working on. I could find only FIND FIRST in most of the places. :(
The habitual use of FIND FIRST infects far too many Progress applications.

Unfortunately it is unlikely that you will make any headway stamping it out. Sadly, it is far more likely that you will be told that it is the right way to do things :(
 
#8
Tom, Casper,

Completely agree with you this time :lol:. Actually I do not have a habit of using the by phrase on non indexed fields, so I have never stumbled accros this bad habit of the FOR FIRST/LAST. I do agree that it is always a bad habit of using FOR FIRST/FIND FIRST in the first place unless there is no other option available for your requirements.
 
#9
Hi

The difference is following

1. For start a loop while find does not.
2. Find can not use multiple indexes while for can use.

In case we does not have need to use multiple index and single record always use find otherwise use for.
 
#10
Hi Guys,
...got the xact difference between FOR FIRST and FIND FIRST as well as its hazards. Tonnes of thanks to u all who participated in this thread.

Very nice to see these many number of quick replies. It is really amazing to read ur replies and the way u r responding. Very happy to be a member among such a group.

Thanks a lot to progresstalk.com as well... :party2: