Code examples · Modern ABAP · Intermediate
ABAP field symbols and data references: change rows in place and read any structure
The problem
A price table must get a 10 percent increase without copying each row.
How we solve it
- 1In placeASSIGNING
- 2GenericRTTI, ASSIGN COMPONENT
- 3DynamicCREATE DATA
The result
Both prices are raised in the table, the first row is printed field by field and the dynamic variable holds its value.
1. The requirement
A price table must get a 10 percent increase without copying each row. A support tool must print the field names and values of any structure it receives. Another routine needs a number variable whose type is only known at runtime.
2. How we design it
Three tools solve the three tasks. A field symbol is an alias to a row in the table, so changing it changes the table. The RTTI class CL_ABAP_STRUCTDESCR describes a structure at runtime, and ASSIGN COMPONENT reaches each field by name. CREATE DATA builds a variable from a type that is only known while the program runs, and a data reference points to it.
Why we decided it this way
- 1ASSIGNING instead of INTO for changesINTO copies the row. A change to the copy is lost unless you write it back. ASSIGNING works on the row itself.
- 2RTTI for the field namesThe structure itself does not know its field names at runtime. The type description does.
- 3Check that the assignment workedA field symbol that is not assigned causes a short dump when used. IS ASSIGNED prevents that.
Before you start
- Basic ABAP: internal tables and string templates.
3. The code, step by step
Step 1 of 3: Change table rows in place
Raise every price by 10 percent without copying a row.
" Class frame and a price table
CLASS zcl_field_symbols_demo DEFINITION PUBLIC FINAL CREATE PUBLIC.
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun.
ENDCLASS.
CLASS zcl_field_symbols_demo IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
TYPES: BEGIN OF ty_price,
item TYPE c LENGTH 1,
price TYPE p LENGTH 8 DECIMALS 2,
END OF ty_price,
ty_prices TYPE STANDARD TABLE OF ty_price WITH EMPTY KEY.
DATA(prices) = VALUE ty_prices(
( item = 'A' price = '10.00' )
( item = 'B' price = '20.00' ) ).
" LOOP AT ... ASSIGNING
LOOP AT prices ASSIGNING FIELD-SYMBOL(<price>).
<price>-price = <price>-price * 11 / 10.
out->write( |{ <price>-item } { <price>-price }| ).
ENDLOOP.
Part by part (plain text)
- Class frame and a price table. A runnable class with a small structure type and a table with two items. Prices are packed numbers with two decimals.
- LOOP AT ... ASSIGNING. FIELD-SYMBOL(<price>) is an alias for the current row. Writing to <price>-price writes into the table at once. The price is multiplied by 11 and divided by 10, which gives exactly 10 percent more without rounding problems.
Open in the portal
The SAP objects named in this code are linked in the code above and listed here. Each opens its page in the SAP Library.
If it does not work
4. The final outcome
Both prices are raised in the table, the first row is printed field by field and the dynamic variable holds its value.
What you should see
A 11.00 B 22.00 ITEM = A PRICE = 11.00 Dynamic value 5.50
Try it and check the result
- Activate and run with F9. The console shows the five lines of the outcome.
- In the LOOP replace ASSIGNING FIELD-SYMBOL(<price>) with INTO DATA(price) and adapt the names: the new prices are no longer stored in the table, which shows why ASSIGNING matters.
- Change ASSIGN prices[ 1 ] to prices[ 5 ]: the IF skips the block instead of ending in a dump.
Common mistakes
- Using a field symbol that is not assigned ends in a runtime error. Test IS ASSIGNED whenever the ASSIGN can fail.
- A field symbol that points into a table becomes invalid if the row is deleted. Do not keep it across table changes.
- Dynamic code is checked at runtime, not by the compiler. Use it only where the type truly is unknown, and prefer typed code everywhere else.