Sunday, February 28, 2010

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 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.

 


 

Matthew Billingham
Source: http://searchsap.techtarget.com/tip/0,289483,sid21_gci1385482,00.html?track=sy191&utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+techtarget%2Fsearchsap%2Fsapnews+%28SearchSAP+%3A+SAP+news%2C+tips+and+expert+advice%29&utm_content=Google+Reader


 

No comments:

Post a Comment