Buffer-Copy

Kalan

Member
Hi All,

Inside the internal proc. - For each blk, if we use buffer-copy without presiding create statement, buffer-copy creates the record only once and its overwrite for every iteration. It means for each block does not release the target buffer and in the second iteration its buffer copying/over writing the source record into target table buffer without triggering the create buffer internally. Does it known issue? Could anyone can explain the reason behind this record scope?

Thanks,
 

mrobles

Member
This is due the scope and logic.
If your code is like this ...
CREATE X.
FOR EACH Y.
BUFFER-COPY Y TO X.
END.
then you only create a record, but
If your code is like this ...

FOR EACH Y.
CREATE X.
BUFFER-COPY Y TO X.
END.
then you create a X record and is filled in each iteration.
 

Kalan

Member
Thanks, Its not mandatory that we need to use create statement before buffer-copy. If we dont explicitly mention the create smt, buffer-copy internally checks the target record exist or not... if not its create the record and execute the buffer-copy. Below for each blk will work well if we invoke this as an external proc. My query is why the record scope is different in these 2 different scenario?

CREATE X.
FOR EACH Y.
BUFFER-COPY Y TO X.
END.
 

TomBascom

Curmudgeon
That implicit create feature was a new one on me. Very clever. I'm glad I didn't post my knee-jerk reaction ;)

"If none already exists" is the key. In your example one already exists. There is a buffer X in scope. So it will be used. The fact that it gets reused over and over is a result of your program structure inter-acting with the record scope.

If you carefully move the BUFFER-COPY into a procedure and scope the buffer to your IP rather than the main procedure then no buffer X will exist with each iteration and so a new one will be created:

Code:
define temp-table cx like customer.

procedure z:
  define buffer cx for cx. /* this keeps cx buffer scoped to procedure z and keeps it hidden from the main procedure */
  buffer-copy customer to cx.
end.

for each customer:
  run z.
end.

for each cx:
  display cx.name.
end.
 

RealHeavyDude

Well-Known Member
IMHO - I always like my code NOT to be relying to much on not so obvious behavior that can potentially be influenced by other things like buffer scope. Therefore I always recommend to use named buffers and strong scoping especially when modifying Temp-Tables. Furthermore I would not omit the CREATE statement before the BUFFER-COPY although it might work under specific circumstances - but that's again IMHO because I think it gives you code that is better read and understandable. That's also why I never use the OF shortcut when joining buffers in a query.

Heavy Regards, RealHeavyDude.
 
Top