0

I am new in ABAP and I have to modify these lines of code:

LOOP AT t_abc ASSIGNING <fs_abc> WHERE lgart = xyz.
  g_abc-lkj = g_abc-lkj + <fs_abc>-abc.
ENDLOOP.

A coworker told me that I have to use a structure and not a field symbol.

How will be the syntax and why to use a structure in this case?

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Gme Moreno
  • 19
  • 2
  • 6

2 Answers2

4

I have no idea why the co-worker wants that you use a structure in this case, because using a field symbol while looping is usually more performant. The reason could be that you are doing some kind of a novice training and he wants you to learn different syntax variants.

Using a structure while looping would like this

LOOP AT t_abc INTO DATA(ls_abc)
  WHERE lgart = xyz.
  g_abc-lkj = g_abc-lkj + ls_abc-abc.
ENDLOOP.
Jagger
  • 10,350
  • 9
  • 51
  • 93
  • Other reason could be that T_ABC has only a few small fields in which case the use of a structure is more efficient than the field symbol. – Gert Beukema May 19 '16 at 17:22
  • 2
    Are you sure? Is there a benchmark that proves it? I would imagine that with a simple structure there is not much of a gain with using a field symbol but I would still think that this is a bit faster approach. – Jagger May 19 '16 at 17:23
  • 2
    No, I was not sure, I read it somewhere in SAP documentation I believe. I have now tested it and it is just not true. With a table with just one field of type I or C with length 1 the LOOP INTO takes about 30% more time than the LOOP ASSIGNING, with larger table entries this difference only get bigger. Number of entries in the table also seems to have an impact, when I increased the number of entries in the table from 1,000 to 10,000 the advantage almost complete disappeared, however the LOOP ASSIGNING was always faster. So, from now on I'll use LOOP ASSIGNING with no worries. – Gert Beukema May 20 '16 at 22:01
0

Your code is correct, because Field symbol functions almost the same as a structure.

For Field symbol

  • Field symbol is a pointer,
  • so there is no data copy action for field symbol, and the performance is better
  • Well if we changed the value via field symbol, the internal table get changed also

For Structure

  • Structure is a copy of the data, so there is a data copy action, and the performance is bad if the data row is bigger than 200 bytes (based on the SAP ABAP programming guide for performance)
  • If changed the data in the structure, the original internal table remains the same because there are 2 copies of the data in memory
Klaus Hopp
  • 41
  • 2