Using PRESELECT EACH

Coop

New Member
I would not describe it that way.

PRESELECT makes a snapshot of the result set at the point in time that it is first invoked. To access the records in the snapshot you use FIND.

How it does that behind the scenes is not the point of my comments.
Any help would be greatly appreciated.
I am grappling with this PRESELECT statement. Never understood or trusted it. I have done this several times over the years and I am trying to find a better way.

I need to duplicate a set of recs to the same table so:
rec.fld1 is the only index in this particular case.
Example:
Code:
def buffer brec for rec.
for each rec no-lock where rec.fld1 = "xyz" and rec.fld2 = "xyz":
  buffer-copy rec to brec.
  assign brec.fld1 = "abc"
             brec.fld2 = "abc".
end.
It turns into an infinite loop because I am creating new records within the for each loop. I thought PRESELECT might be the magic bullet for this but it doesn't appear to be. I finally went to the trouble to test it and PRESELECT seems to behave exactly as a "FOR EACH". They both do NOT display the new record in "LOOP1" and do display in "LOOP2" - as expected in LOOP2 which is just to verify the record was created. I would have expected the new record to show up immediately at the end of "LOOP1" when using "FOR EACH" and for it to NOT show up when using "PRESELECT" but it doesn't show for either which is just baffling - I know the record is there.
Am I missing something? Obviously I am but is there a way?? Without having to loop through and gather the rowids and loop through those in a separate loop?

Actual code I used to test:
Record is deleted at top and created in the loop when i=5.
I comment/uncomment the "for each/preselect" code.
zcomp.measid is the only index.
Code:
def var i as int no-undo.
def buffer bzcomp for zcomp.

for each zcomp where zcomp.char2 = 'div12'
                 and zcomp.measid= 'zzz'.
  disp 'delete loop' zcomp.char2 zcomp.measid.
  delete zcomp.
end.
/*
REPEAT PRESELECT EACH zcomp where zcomp.char2  = 'div12' no-lock:
  find next zcomp.
*/
FOR EACH zcomp where zcomp.char2  = 'div12' no-lock:

  i = i + 1.
  disp 'LOOP1'
       trim(string(i,'>>>')) label 'cnt'
       zcomp.char2
       zcomp.measid.
  if i=5 then do:
    create bzcomp.
    assign bzcomp.char2='div12'
           bzcomp.measid='zzz'.
  end.
end.

i = 0.
for each zcomp where zcomp.char2  = 'div12' no-lock:
  i = i + 1.
  disp 'LOOP2'
       trim(string(i,'>>>')) label 'cnt'
       zcomp.char2
       zcomp.measid.
end.
 
Last edited by a moderator:
Top