Field symbols are similar to pointers in C language, field symbols does not have any memory instead they will be pointing to a memory location.
The concept of field symbols is very important in order to increase the performance of SAP applications, but unethical use of field-symbols leads to application issues.
Field symbol name should always be within<>
, example:
.Syntax for declaring a field symbol.
FIELD-SYMBOLS : <FIELD_SYMBOL> TYPE MARA-MATNR. "here MARA-MATNR is a variable type FIELD-SYMBOLS : <FIELD_SYMBOL> TYPE MARA. "here MARA is a structure FIELD-SYMBOLS : <FIELD_SYMBOL> TYPE REF TO DATA . "here DATA is a reference type
ASSIGNING
and ASSIGN
are the keywords which are used to assign a value to the field symbol.Example of using field symbol as work area
In the below example we are going to use field symbol as work area.REPORT ZSAPN_FIELDSYMBOLS. DATA : IT_MARA TYPE TABLE OF MARA. DATA : WA_MARA TYPE MARA. FIELD-SYMBOLS : <FS_MARA> TYPE MARA. SELECT * FROM MARA INTO TABLE IT_MARA UP TO 50 ROWS. LOOP AT IT_MARA ASSIGNING <FS_MARA>. IF <FS_MARA> IS ASSIGNED. WRITE :/ <FS_MARA>-MATNR, <FS_MARA>-MTART, <FS_MARA>-MEINS. ENDIF. ENDLOOP.
Check weather the field symbol is assigned or not before processing field symbol, it is very important to check field symbol, if it is not assigned it will get a run-time error, use the blow syntax to check field symbol assignment.
IF <FS_MARA> IS ASSIGNED. **process field symbol ENDIF.
Increase performance using field symbols in SAP ABAP
I belive there are always performance differences between reference variables(work area) and memory pointing(field symbols), here I wants to add some proof for that.
Below you will find two programs one with work area and another one with field symbols, execute them and see time difference at the end of the program( will display at the bottom of output), you will find difference.
program1REPORT ZSAPN_FIELDSYMBOLS1. DATA : IT_MARA TYPE TABLE OF MARA. "internal table FIELD-SYMBOLS : <FS_MARA> TYPE MARA. "field symbols DATA : LV_TIME_START TYPE TIMESTAMPL. "time at start of loop DATA : LV_TIME_END TYPE TIMESTAMPL. "time at the end of loop DATA LV_TIME_TAKEN TYPE P DECIMALS 8. "time difference SELECT * FROM MARA "select data from MARA INTO TABLE IT_MARA UP TO 20000 ROWS. GET TIME STAMP FIELD LV_TIME_START. "get time at start of loop LOOP AT IT_MARA ASSIGNING <FS_MARA>. "loop start WRITE :/ <FS_MARA>-MATNR, <FS_MARA>-MTART, <FS_MARA>-MEINS. ENDLOOP. GET TIME STAMP FIELD LV_TIME_END. "get time at the end of loop LV_TIME_TAKEN = LV_TIME_END - LV_TIME_START. "time taken WRITE:/ 'Time taken: ', LV_TIME_TAKEN. "display time takenProgram2
REPORT ZSAPN_FIELDSYMBOLS2. DATA : IT_MARA TYPE TABLE OF MARA. "internal table DATA : WA_MARA TYPE MARA. "work area DATA : LV_TIME_START TYPE TIMESTAMPL. "time at start of loop DATA : LV_TIME_END TYPE TIMESTAMPL. "time at the end of loop DATA LV_TIME_TAKEN TYPE P DECIMALS 8. "time difference SELECT * FROM MARA "select data from MARA INTO TABLE IT_MARA UP TO 20000 ROWS. GET TIME STAMP FIELD LV_TIME_START. "get time at start of loop LOOP AT IT_MARA INTO WA_MARA. WRITE :/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS. ENDLOOP. GET TIME STAMP FIELD LV_TIME_END. "get time at the end of loop LV_TIME_TAKEN = LV_TIME_END - LV_TIME_START. "time taken WRITE:/ 'Time taken: ', LV_TIME_TAKEN. "display time taken
See the time take to process loop in both programs.