sapdev logo background
sapdev logo sapdev logo
Comments

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




The field symbol has so many uses and if used correctly can often lead to simpler to code to write. Although I do admit it is not always easiest to read at first if you don't have much field symbol experience. The simplest use for a FIELD-SYMBOL is to point to another field so that you can update it directly without moving data around all over the place. One example of this would be within an internal table LOOP. Instead of updating the data within a work area and then using the modify statement to update the table you can simply assign the data of the current loop pass to a field symbol. This then allows you to update the table record directly. See here for more details of the ABAP modify statement

The resultant field symbol based modify code would look something like this

DATA: it_database type STANDARD TABLE OF MARA.
FIELD-SYMBOLS: <fs_itab> LIKE LINE OF it_database.
LOOP AT it_database ASSIGNING .
  -field1 = 'New Value'.
ENDLOOP.

..But the above is really a personal thing and I am not sure it will really make too much difference to the overall performance. It is always worth giving it a test though if you are having issues with performance of a specific report.

Reference fields dynamically on the fly using a FIELD-SYMBOL

Field symbols offer so much more though allowing you to not only reference fields as above but also 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 it might not be too clear what i mean there but bare with me and hopfully by the time you see the example below it might make a bit more sense... Imagine you had a list of field names stored in a table (i.e. ld_field2, gd_tot ...), you would be able to loop around this table and access the actual fields.

So for this very simple example field symbol would contain the value 'LD_FIELD2' but would contain the value 'hello'. i.e. the value of 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>.

Hope your still with me, if not below is an example executable ABAP program that uses field symbols to hopefully show how they work in more detail! Just copy it into you own program and debug it.

*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.



comments powered by Disqus