ABAP Field Symbols usage – Advanced use

By | Last Updated on April 15, 2013 | 68,960

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.

In this second part of the FS usage code snippet, check out the advanced usage – Like Copy ITAB to ITAB, Dynamic Component access. Check out ABAP Field Symbols usage for first part of this.

Copy Internal Tables using two field-symbols

 
DATA: lt_1 TYPE STANDARD TABLE OF t100.
DATA: lt_2 TYPE STANDARD TABLE OF t100.
FIELD-SYMBOLS: <lt_1> TYPE ANY TABLE.
FIELD-SYMBOLS: <lt_2> TYPE ANY TABLE.
*
ASSIGN lt_1 TO <lt_1>.
*
SELECT * FROM t100
  INTO TABLE <lt_1>
  UP TO 10 ROWS.
*
ASSIGN lt_2 TO <lt_2>.
<lt_2> = <lt_1>.
*
DATA: lv_lines TYPE i.
lv_lines = LINES( lt_2 ).
WRITE: lv_lines.
 

Assign data using two field-symbols

 
DATA: lt_1 TYPE STANDARD TABLE OF t100.
DATA: lt_2 TYPE STANDARD TABLE OF t100.
FIELD-SYMBOLS: <ls_1> TYPE t100.
FIELD-SYMBOLS: <ls_2> TYPE t100.
*
SELECT * FROM t100
  INTO TABLE lt_1
  UP TO 10 ROWS.
*
LOOP AT lt_1 ASSIGNING <ls_1>.
  APPEND INITIAL LINE TO lt_2 ASSIGNING <ls_2>.
  <ls_2> = <ls_1>.
ENDLOOP.
*
DATA: lv_lines TYPE i.
lv_lines = LINES( lt_2 ).
WRITE: lv_lines.
 

Field Symbol to access component by Name

 
DATA: lt_1 TYPE STANDARD TABLE OF t100.
FIELD-SYMBOLS: <ls_1> LIKE LINE OF lt_1.
*
SELECT * FROM t100
  INTO TABLE lt_1
  UP TO 10 ROWS.
*
* dynamic access
DATA: lv_field TYPE char30.
FIELD-SYMBOLS: <lv_field> TYPE ANY.
lv_field = 'TEXT'.
LOOP AT lt_1 ASSIGNING <ls_1>.
  ASSIGN COMPONENT lv_field OF STRUCTURE <ls_1> TO <lv_field>.
  CHECK <lv_field> IS ASSIGNED.
  WRITE: / <lv_field>.
ENDLOOP.
 

Field Symbol to access component by Component Number

DATA: lt_1 TYPE STANDARD TABLE OF t100.
FIELD-SYMBOLS: <ls_1> LIKE LINE OF lt_1.
*
SELECT * FROM t100
  INTO TABLE lt_1
  UP TO 10 ROWS.
*
* dynamic access
DATA: lv_comp_number TYPE i.
FIELD-SYMBOLS: <lv_field> TYPE ANY.
lv_comp_number = 4.
LOOP AT lt_1 ASSIGNING <ls_1>.
  ASSIGN COMPONENT lv_comp_number OF STRUCTURE <ls_1> TO <lv_field>.
  CHECK <lv_field> IS ASSIGNED.
  WRITE: / <lv_field>.
ENDLOOP.
 

 

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

2 Comments

  • steve oldner

    I admit using “access component” give me problems. To me, it doesn’t logically read. So I have code examples I use whenever I need to use something like this. AND I have a helper class that I’ve coded this so I don’t need to re-think this each time. Now I will print this and hang it on my cubicle wall.

    Why didn’t you use an unassign?

  • Assign component based on name is very useful in APO world, where data is in name – value format.

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