Ads

Use of field symbols in SAP ABAP programming, using field symbols in SAP ABAP

Use of field symbols in SAP ABAP programming, using field symbols in SAP ABAP 

the declaration operator FIELD-SYMBOL declares a field symbol <fs> to which a memory area is assigned in the current operand position. The declared field symbol is visible statically in the program from FIELD-SYMBOL(<fs>) and is valid in the current context. The declaration is made when the program is compiled, regardless of whether the statement is actually executed.

The 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 that are used to assign a value to the field symbol.

Example of using field symbol as a work area


In the below example we are going to use the field symbol as a 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 40 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 whether the field symbol is assigned or not before processing the field symbol, it is very important to check the field symbol, if it is not assigned it will get a run-time error, use the below syntax to check the field symbol assignment.

  IF <FS_MARA> IS  ASSIGNED.
**process field symbol
  ENDIF.

Increase performance using field symbols in SAP ABAP


I believe there are always performance differences between reference variables(work area) and memory pointing(field symbols), here I want to add some proof for that.

Below you will find two programs one with a work area and another one with field symbols, execute them and see the time difference at the end of the program( will display at the bottom of output), you will find differences.

program1
REPORT 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 the start of the loop

DATA: LV_TIME_END TYPE TIMESTAMPL. "time at the end of the loop.




Comments

Popular posts from this blog

BADI Interview Questions in SAP ABAP

Sample SAP ABAP Programming Examples for Practice

Module Pool Programming Interview Questions and Answers in SAP ABAP

Step by Step tutorial on BDC Session Method Program in SAP ABAP

SAP ABAP Interview Questions and Answers for 10 Years Experienced