ABAPLOOP_AT_ITAB - LOOP AT ITAB

Fill RESBD Structure from EBP Component Structure   ROGBILLS - Synchronize billing plans  
This documentation is copyright by SAP AG.

LOOP AT itab

Short Reference



Syntax

LOOP AT itab result[cond].
  ...
ENDLOOP.

Effect

The LOOP and ENDLOOP statements define a loop around a statement block. The LOOP statement reads lines from the internal table itab sequentially.

  • The output response result determines how and to where the row contents are read.

  • The table key with which the loop is executed can be determined in cond.You can either read all the lines or specify cond conditions to restrict which lines to read.

The statement block between LOOP and ENDLOOP is executed once for each line. To exit processing of the statement block, you can use the statements described in theleave loops section.

If no explicit table key name is declared after USINGKEY, the order in which the lines are read depends on the table type as follows:

  • Standard tables and sorted tables
    The lines are read by ascending line numbers in theprimary table index.

  • Hashed tables
    The lines are processed in the sequence in which they were inserted in the table, and by the sort sequence with the SORT statement.

The loop continues to run until all the table lines that meet the cond condition have been reador until it is exited with a statement. If no appropriate lines are found or if the internal table is blank, the loop is not run at all.

System fields

During each loop run for index tables, and when you are using asortedkey, the statement LOOP AT sets the value of the system field sy-tabix to the line number of the current line in the relevant table index. Inhashed tables and when using ahashkey sy-tabix is set to the value 0. LOOP AT does not modify sy-subrc. Afterleaving the loop using ENDLOOP, sy-tabix is set to the value that it had before entering the loop and that applies for sy-subrc:

sy-subrcMeaning
0The loop was run at least once.
4The loop was not run at all.

The system fields sy-tfill and sy-tleng are also filled.

Changing internal tables in a loop

If you insert or delete lines in the statement block of a LOOP, this will have the followingeffects: The position of inserted or deleted lines with regard to the current line is determined bythe line numbers in the corresponding table index in the case of loops on index tables or if using asorted key. In the case of loops on hashed tables and if using a hash key, the position depends on the insertion sequence.

  • If you insert lines after the current line, these new lines will be processed in the subsequent loop passes. An endless loop can result.

  • If you delete lines after the current line, the deleted lines will no longer be processed in the subsequent loop passes.

  • If you insert lines before the current line, the internal loop counter is increased by one with eachinserted line. This affects sy-tabix, which is also increased (in the subsequent loop pass in the case of loops on index tables or when using a sorted key).

  • If you delete the current line or lines before the current line, the internal loop counter is decreasedby one with each deleted line. In the case of loops on index tables or if using a sorted key, this affects sy-tabix in the subsequent loop pass, and sy-tabix is decreased accordingly.

The replacement of the entire table body in a LOOP using this table causes the loop to be exitedat the next loop pass in accordance with the rules described above. This is particularly the case ifnew rows were added to the table afterwards. Since this usually leads to unpredictable program behavior,you cannot access the entire table body in change mode in a loop. If this is statically discernible,a syntax error occurs in classes and for LOOPS with statically discernible secondary keys. Otherwise,the syntax check simply returns a warning for compatibility reasons. However, at runtime, a runtimeerror always occurs in the case of the replacement of the entire table body with statements such as CLEAR, FREE,LOCAL, REFRESH,SORT, DELETE... WHERE, and with all types of assignments to itab.

Notes

  • If the internal table itab is specified using areferencevariable, the loop is executed completely using the table referenced at entry. Any changes to the reference variable do not have an effect on the loop. The associated object cannot be deleted from theGarbageCollector until the loop has been completed. The same thing is true if the table is representedby a field symbol. After the implementation of the field symbol in the loop, iteration still takes place using the table linked to the field symbol when LOOP is entered.
  • There is no implicit selection of a suitable key or index. The used table key or table index is always specified uniquely. The syntax check issues a warning if there is a suitablesecondarytable key but this table key is not used. This warning should be removed through using the key. However, in exceptional cases, it can be bypassed using apragma.


Example

Nested LOOP loops without explicit key declaration. . The contents of the current row for the outer loop are analyzed in the WHERE condition for the inner loop.

PARAMETERS p_name TYPE scarr-carrname DEFAULT '*'.

DATA: scarr_tab TYPE SORTED TABLE OF scarr
                WITH UNIQUE KEY carrname,
      spfli_tab TYPE SORTED TABLE OF spfli
               WITH NON-UNIQUE KEY carrid.

FIELD-SYMBOLS <scarr_line> LIKE LINE OF scarr_tab.
DATA spfli_line LIKE LINE OF spfli_tab.

SELECT *
       FROM scarr
       INTO TABLE scarr_tab.

SELECT *
       FROM spfli
       INTO TABLE spfli_tab.

LOOP AT scarr_tab ASSIGNING <scarr_line>
                  WHERE carrname CP p_name.
  LOOP AT spfli_tab INTO spfli_line
                   WHERE carrid = <scarr_line>-carrid.
    WRITE: / spfli_line-carrid,
             spfli_line-connid.
  ENDLOOP.
ENDLOOP.



Example

The following loop deletes all lines in an internal table, since the short form of the DELETEstatement always deletes the current first line.

DATA itab TYPE TABLE OF i.
DATA wa LIKE LINE OF itab.

LOOP AT itab INTO wa TO 6.
  DELETE itab.
ENDLOOP.

Exceptions

Catchable Exceptions

CX_SY_ITAB_DYN_LOOP

  • Cause: Error in a dynamic WHERE condition
    Runtime Error: DYN_WHERE_PARSE_ERROR

Non-Catchable Exceptions

  • Cause: Illegal conversion of the LOOP field symbol in the core of the loop.
    Runtime Error: ITAB_ILLEGAL_REG
  • Cause: Illegal assignment to the LOOP reference in the core of the loop.
    Runtime Error: MOVE_TO_LOOP_REF
  • Cause: Unpermitted change of entire table body in the loop
    Runtime Error: TABLE_FREE_IN_LOOP







ROGBILLS - Synchronize billing plans   TXBHW - Original Tax Base Amount in Local Currency  
This documentation is copyright by SAP AG.


Length: 11609 Date: 20120523 Time: 000609     triton ( 264 ms )