ABAP FIELD SYMBOL - Techniques for manupulating data using the FIELD-SYMBOL statement


The most useful thing a field symbol allows you to do is reference any field/variable on the fly by just putting the name of a field into a variable and assigning a field symbol to it. Sorry this is proving more difficult to describe than I thought it would be but one use for this could be if you had a list of field names stored in a table, you would be able to loop around that table and access the fields stored in it. So for the following simple example code would contain the value ‘hello’ and would contain the value ‘LD_FIELD2’

Data: ld_field1 type string.
Data: ld_field2 type string.
FIELD-SYMBOLS: <fs1>, <fs2>.

concatenate ‘LD_FIELD2’ into ld_field1.
Ld_field2 = ‘hello’.

ASSIGN (ld_field1) TO <fs1>.
ASSIGN ld_field1 TO <fs2>.

Below is an example executable ABAP program that uses field symbols to hopefully should how field symbols work in more detail!

*Code to demonstrate field-symbols

REPORT  zfield_symbols                                                 .

TYPES: BEGIN OF t_p0121,
 pernr TYPE pa0121-pernr,
 rfp01 TYPE pa0121-rfp01,
 rfp02 TYPE pa0121-rfp02,
 rfp03 TYPE pa0121-rfp03,
 rfp04 TYPE pa0121-rfp04,

 END OF t_p0121.

DATA: it_p0121 TYPE STANDARD TABLE OF t_p0121 INITIAL SIZE 0,
      wa_p0121 TYPE t_p0121.

DATA: gd_index TYPE string,
      gd_rfp0 TYPE string.

FIELD-SYMBOLS: <fs1>, <fs2>.

****************************************************************
*Start-of-selection.
START-OF-SELECTION.

  SELECT pernr
         rfp01
         rfp02
         rfp03
         rfp04
   UP TO 10 ROWS
    FROM pa0121
    INTO TABLE it_p0121.

****************************************************************
*End-of-selection.
END-OF-SELECTION.

WA_P0121-RFP01 = '1234'.
CONCATENATE 'WA_P0121-RFP01'  gd_index INTO gd_rfp0.

* Now watch how the values change as you loop around the table fields
  LOOP AT it_p0121 INTO wa_p0121.
    write:/.
    write:/ wa_p0121-pernr.
    CLEAR: gd_index.
    DO.
      gd_index = gd_index + 1.
      CONCATENATE 'WA_P0121-RFP0'  gd_index INTO gd_rfp0.

* assign with brackets
      ASSIGN (gd_rfp0) TO <fs1>.  "assigns the value of field name contained in variable
* fs1 value would be the value of the field WA_P0121-RFP01..21
* i.e. if index 1 and WA_P0121-RFP01 = 1234 then fs1 would = 1234

* assign without brackets
      ASSIGN gd_rfp0 TO <fs2>. " assigns the exact value contained in the field
* fs1 value would literally be the same as the field WA_P0121-RFP01..21
* i.e. if index 1 then fs2 would = 'WA_P0121-RFP01'
*         index 2 then fs2 would = 'WA_P0121-RFP02' etc...

* you may also notice that once assigned any change made to the field gd_rfp0
* is instantly reflected in the field symbol (fs2) so technically you could perform
* the assign command once outside of the loop, but i have left it here to aid
* readability.
      write:/ <fs2>, <fs1>.
      IF gd_index GE 21. "exit once last field has been read
        EXIT.
      ENDIF.
    ENDDO.
  ENDLOOP.