FOR EACH Vs FOR FIRST in Record Scoping

Hello Guys, hope you all are well.

I was surprised to see that For First behaves different from For each. Each iteration of For each is a transaction (in X lock) and after end of For Each, record scope is not available.

But with For First, scope is available and lock is downgraded to Shared lock. Checked for Promon. First and Each are just attributes of For block then why is that difference. Please suggest.

Thanks and Regards,
Learner
 

TomBascom

Curmudgeon
I suggest that you should never, ever, use FOR FIRST.

If you LEAVE a FOR FIRST or FOR EACH (perhaps after just finding the first record) the buffer will also still be in scope.

Code:
for first customer no-lock by discount:
  leave.
end.

display name.
pause.

for each customer no-lock by discount:
  leave.
end.

display name.
pause.
 
Hi Tom, Thanks for your reply!

Any specific reason why I should never user For First, one reason I could think of is, record scope is available after End of For First.

Code:
def var iLoop as int no-undo.

For First Customer no-lock
  where Customer.custnum < 5:

  iLoop = iLoop + 1.

  message Customer.custnum + iLoop.
  pause. 

END.

Message "Out of For First".
pause.

/* If we check here then record is still available in buffer with Shared lock but If we change First with Each (For Each) then no record buffer available */

Please suggest.

Thanks and Regards,
Learner
 

TomBascom

Curmudgeon
The explanation regarding why you should not use FOR FIRST is in the thread linked in my first post. Click on the blue text.
 

TomBascom

Curmudgeon
Regarding the specific question about the buffer being available outside of the FOR FIRST... FOR is a WEAK scoped block. Any free reference to the buffer outside of that block will raise the scope of the buffer. I expect that your actual code refers to the buffer in question somewhere outside of the FOR FIRST block and that is why the scope has been raised. But you didn't share the actual code so I'm just guessing.
 

susieq_va99

New Member
This is a good discussion. When I worked for QAD, they required us to use For First, in many cases since there was only 1 record to find.
 

TomBascom

Curmudgeon
My understanding is that QAD did that and some other kind of strange things primarily to support the Oracle Dataserver.
 

TomBascom

Curmudgeon
In the case where there is exactly one unique record neither FIND FIRST nor FOR FIRST should be used.

"FIRST" is not adding any benefit to the statement.
 
Top