Wednesday, April 24, 2013

Iterating On a Table Using Field Symbols


Say you have a Table called “table_name”, of line type “table_row”, and you wish to iterate on it and perhaps change the rows while you’re iterating. You can do it in ABAP using field symbols.
First, you need to define the field symbol. In our example it’s called “symbol_name”


FIELD-SYMBOLS <symbol_name> TYPE table_row.


Now, you can iterate over the table, assigning each row to the field symbol


LOOP AT table_name ASSIGNING <symbol_name>.
...
ENDLOOP


On each iteration, you can manipulate the table using the field symbol. Any change done to the field symbol is also done on the table. It’s like using a pointer in C or a reference variable in C++.

Important note: When using field symbols on sorted orhashed table, then you may not change the key fields of these tables. Doing so results in a runtime error. The logic is simple: changing the key will change the sorting order or hashing order of the table, and since you’re in mid-iteration, it may cause you to re-iterate on these rows. You Java developers out there may be familiar with this concept, as it also exists in Java.

No comments:

Post a Comment