ABAP Field Symbols usage

By | Last Updated on April 8, 2013 | 70,179

ABAP Field Symbols are very powerful. Usage of Field-Symbols would provide performance gain, but for that you must use it correctly otherwise you may run into issues.

Field-symbols can provide great performance over using workarea as I have mentioned in my earlier article – Use of Field-symbols vs Work area. I have mentioned many articles using Field-Symbols but wanted to note down the basics as part of this code snippet.

Declare a Field Symbols

 
TYPES: tt_mara TYPE STANDARD TABLE OF mara.
DATA:  t_mara TYPE tt_mara.
*
FIELD-SYMBOLS: <lfs_mara> LIKE LINE OF t_mara.  " <<
*
" Field symbol without type
FIELD-SYMBOLS:
  <lfs_any_tab> TYPE ANY TABLE,
  <lfs_any> TYPE ANY.
 

APPEND and INSERT using Field Symbols

 
* Append line
APPEND INITIAL LINE TO t_mara ASSIGNING <lfs_mara>.
<lfs_mara>-matnr = '123456'.
*
* insert table
INSERT INITIAL LINE INTO t_mara ASSIGNING <lfs_mara> INDEX 2.
<lfs_mara>-matnr = 'ABCDEF'.
 

Access ITAB rows using Field Symbols

 
* Read table
READ TABLE t_mara ASSIGNING <lfs_mara>
  WITH KEY matnr = '123456'.
IF sy-subrc EQ 0.
  WRITE: <lfs_mara>-matnr.
ENDIF.
*
* Access via Loop
LOOP AT t_mara ASSIGNING <lfs_mara>.
  WRITE: <lfs_mara>-matnr.
ENDLOOP.
 
 

Modify an entry using Field-Symbols:

 
* READ and MODIFY
READ TABLE t_mara ASSIGNING <lfs_mara>
  WITH KEY matnr = '123456'.
IF sy-subrc EQ 0.
  <lfs_mara>-ersda = sy-datum.
ENDIF.
*
* LOOP and MODIFY
LOOP AT t_mara ASSIGNING <lfs_mara>.
  <lfs_mara>-ersda = sy-datum + 1
ENDLOOP.
 

CHECK field Symbols using IS ASSIGNED

Check if Field Symbol is assigned to a valid reference

 
* Check if Field-Symbol is assigned
IF <lfs_mara> IS ASSIGNED.
  WRITE: 'Assigned'.
ELSE.
  WRITE: 'Unassigned'.
ENDIF.
 

Remove the Reference of the Field Symbols

 
"remvoe the reference
UNASSIGN <lfs_mara>.
 

Field-Symbols on SAP Help
 

Advertisement

Do you have a Code Snippet which you want to share, Submit Code Snippet here

Share It!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{274 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 274 articles.

Load comments

4 Comments

  • Pedro Henrique Parreira

    Your blog is very nice!

    =]

  • Avinash Palavau

    Gud one to give a start to usage of field symbols…

  • Varun Kumar Sahu

    An awesome knowledge on field-symbols. Thank you for giving a very good clear understanding.

  • Hello Padro, Avinash & Varun,

    Glad you guys like the Code snippet on Field Symbols.

    Thanks,
    Naimesh Patel

Comments on this Post are now closed. If you have something important to share, you can always contact me.