LOOP Patterns: ASSIGNING, WHERE, GROUP BY, VALUE and REDUCE
Learn practical looping patterns from classic ABAP to modern ABAP syntax.
Explanation
LOOP AT is the most common internal table processing statement. Classic ABAP uses LOOP AT ... INTO work area. For direct row modification, LOOP AT ... ASSIGNING FIELD-SYMBOL is often better. WHERE conditions can restrict processing to relevant rows. Modern ABAP provides expressions such as VALUE, FOR, FILTER and REDUCE. These can make transformations and aggregations concise, but they should not be used only for style. The best code is readable, safe and suitable for the data volume. For very large datasets, expression-based processing should still be reviewed for memory usage.
Code example
DATA lt_high_value TYPE STANDARD TABLE OF ty_item. lt_high_value = FILTER #( lt_item WHERE netwr > 10000 ). DATA(lv_total) = REDUCE netwr( INIT total = 0 FOR ls_item IN lt_item NEXT total = total + ls_item-netwr ).Real project scenario
A pricing validation report groups sales order items by material and calculates total quantity. Modern expressions like FILTER and REDUCE can make the logic shorter, but for very large datasets the design should still be measured.
Common mistakes
- Using complex expressions that reduce readability. - Looping over full table when WHERE can reduce processing. - Using modern syntax without understanding memory impact. - Mixing too many expressions in one unreadable statement.
Best practices
- Use ASSIGNING for direct row updates. - Use WHERE to limit loop processing when suitable. - Use modern expressions when they improve readability. - Avoid clever code that the team cannot maintain.
Interview angle
A strong answer shows knowledge of both classic and modern ABAP and explains when each is appropriate.