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

..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.
      wa_p0121 TYPE t_p0121.
DATA: gd_index TYPE string,
      gd_rfp0 TYPE string.
FIELD-SYMBOLS: <fs1>, <fs2>.
  SELECT pernr
   UP TO 10 ROWS
    FROM pa0121
    INTO TABLE it_p0121.
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:/ wa_p0121-pernr.
    CLEAR: gd_index.
      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

Related Articles

Beginners Guide to learning SAP development starting with logging into SAP
ABAP Programming EVENTS in SAP
ABAP Function Module basics in SAP
DATA and @DATA Inline ABAP declarations available from release 7.40 to help make your code cleaner and more readable
ABAP Workbench Programming Techniques - BC402
ABAP rules to consider before creating a bespoke abap report or program
ABAP Subroutine basics in SAP
Get access to an SAP system for individual needs
SAP Base 64 encoding and decoding using ABAP code
Call web URl from ABAP report or SAP help documentation
Direct download, downloading sap objects
SAP Icons
SAP icons and some ABAP code to display them
SAP icons list and their associated codes
Internal Program Environment diplays all internal/external objects and operations used by an SAP program
minisap installation on your local pc to allow ABAP development, your own local SE80
SAP minisap installation on your local pc to allow ABAP development for free
SAP module based information such FI, HR, MM, PM, BW etc
Need an SAP ABAP program created?
SAP repository objects - List of useful standard and bespoke SAP repository objects
Retrieve SAP objects using transport entry in SE10 to restore objects that have been deleted
SAP Help for all areas for SAP ABAP Development inc ABAP, ALV, Web dynpro, bsp, HR, BW
ABAP tutorial - Logging into an SAP system for the first time
Manage and delete SAP sessions using ABAP code
SAP module areas
Increase & Decrease size of SAP editor text
ABAP development information and code examples for producing be-spoke SAP functionality
ABAP Development Info - Example code and information on various areas of ABAP development
SAP command field entries - box in top left corner
Force new page when printing abap source code
Hiding ABAP Source Code so that it can not be viewed by anyone
ABAP Internal table declaration - Various methods of creating internal data structures and tables
Parameter ID - ABAP code to demonstrate how to set and get a parameter ID
RANGE statement - Example ABAP code to demonstrate the RANGE command
Change SAP logo in top right hand corner of SAP client
ABAP SELECT statement within SAP - Example ABAP code to demonstrate the SELECT command
Create desktop Shortcut to SAP function
SAP Note assistant - Using transaction SNOTE to read and apply OSS note fix
VARYING command - Example ABAP code to demonstrate the VARYING command
Creating your first helloworld ABAP report in SAP