Tip

Tips on 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

    Requires Free Membership to View

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

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

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:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.