Tips on processing records in SAP internal tables

SearchSAP.com development expert Matthew Billingham shares some of his tips for better processing records in SAP internal tables.

Back in the mists of time (before SAP R/3 4.6c, if I remember correctly), we had two types of tables --tables with header lines and tables without. For tables with header lines, we'd do things like:

LOOP AT my_table_wh.
my_table_wh-somefield = 'some value'.
MODIFY my_table.
ENDLOOP.

For tables without header lines, we'd do things like:

DATA: wa LIKE LINE OF t_table.
LOOP AT t_table INTO wa.
wa-somefield = 'some value'.
MODIFY t_table FROM wa.
ENDLOOP.

Both of these were, in fact, just as quick (or slow) because they both transfer the record from the table into another part of memory and back. With the former, the ABAP runtime does it for you, via the table header. With the latter, you do it yourself. Then we started using LOOP AT … ASSIGNING. This gives us direct access to the records of the table; no more of this tedious mucking about moving a copy of the record into a work area. The consensus seems to be that you should use LOOP AT ASSIGNING only when you're modifying the contents of an internal table. In most situations, it's definitely quicker.

FIELD-SYMBOLS: <ls_wa> LIKE LINE OF t_table.
LOOP AT t_table ASSIGNING <ls_wa>.
<ls_wa>-somefield = 'some value'.
ENDLOOP.

However, there is a scenario I've encountered where using ASSIGNING is slower than INTO. I'm often working with dynamically constructed tables (i.e., Exit Functions in SEM, where I have a table XTH_DATA TYPE HASHED TABLE). When handling these, I could use:

FIELD-SYMBOLS: <ls_data> TYPE ANY,
<l_field> TYPE sometype.
LOOP AT xth_data ASSIGNING <ls_data>.
ASSIGN COMPONENT 'SOMEFIELD' OF STRUCTURE <ls_data> TO <l_field>.
" Do stuff
ENDLOOP.

But I've found, especially if I need to access quite a number of components of the structure and the table has many records, that the following is actually quicker:

FIELD-SYMBOLS: <ls_data> TYPE ANY,
<l_field> TYPE sometype.
DATA: lr_data TYPE REF TO DATA.
CREATE DATA lr_data LIKE LINE OF xth_data.
ASSIGN <lr_data> TO <ls_data>.
ASSIGN COMPONENT 'SOMEFIELD' OF STRUCTURE <ls_data> TO <l_field>.
LOOP AT xth_data INTO <ls_data>.
" Do stuff with <l_field>.
MODIFY TABLE xth_data FROM <ls_data>.
ENDLOOP.


This was first published in February 2010

Dig deeper on SAP ABAP

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchManufacturingERP

SearchOracle

SearchDataManagement

SearchAWS

SearchBusinessAnalytics

SearchCRM

SearchContentManagement

SearchFinancialApplications

Close