Skip to content
IT Canvass
SAP ABAP development · Lesson

Loops

Quick answer

Looping with ASSIGNING FIELD-SYMBOL avoids copying each row into a work area, faster and letting you modify the table row in place.

Key takeaways

  • The single most important ABAP performance rule involves loops: never put a database SELECT inside a loop over many rows.
  • LOOP AT itab: iterate an internal table (the workhorse).
  • DO... ENDDO: a counted or unconditional loop.
  • Watch out: SELECT inside a loop, the top performance killer.

The main loop constructs

  • LOOP AT itab: iterate an internal table (the workhorse).
  • DO ... ENDDO: a counted or unconditional loop.
  • WHILE ... ENDWHILE: loop while a condition holds.

Looping an internal table

LOOP AT lt_orders INTO DATA(ls_order) WHERE status = 'OPEN'.
  " process ls_order
  WRITE: / ls_order-order_id, ls_order-amount.
ENDLOOP.

" modern, field-symbol for performance (no copy)
LOOP AT lt_orders ASSIGNING FIELD-SYMBOL(<ls>).
  <ls>-processed = abap_true.
ENDLOOP.

Performance: the golden rule

The single most important ABAP performance rule involves loops: never put a database SELECT inside a loop over many rows. Read all needed data once (into an internal table) before the loop, then work in memory. A SELECT-in-LOOP over thousands of rows is the classic cause of a slow program.

Field-symbols and references

Looping with ASSIGNING FIELD-SYMBOL avoids copying each row into a work area, faster and letting you modify the table row in place. Prefer it for large tables.

Common pitfalls

  • SELECT inside a loop, the top performance killer.
  • Copying rows when a field-symbol would avoid it.
  • Modifying a table while looping it incorrectly.

Want to learn this properly?

Our live, instructor-led SAP Training covers this hands-on, with real projects and a certification path.

Check your understanding

  1. Which statement is true of Loops?

    • A. Navigating SAP Fiori centers on the launchpad, a personalised, role-based home page of app tiles, and the…
    • B. The SAP GUI (Graphical User Interface) is the classic desktop client for interacting with SAP systems.
    • C. Read all needed data once (into an internal table) before the loop, then work in memory.
    Show answer

    C. Read all needed data once (into an internal table) before the loop, then work in memory.

    Covered in the “Performance: the golden rule” section of this lesson.

  2. Which of these also applies to Loops?

    • A. Learning screens, not the end-to-end process.
    • B. Wrong API for the job (e.g. synchronous where async is needed).
    • C. A SELECT-in-LOOP over thousands of rows is the classic cause of a slow program.
    Show answer

    C. A SELECT-in-LOOP over thousands of rows is the classic cause of a slow program.

    Covered in the “Performance: the golden rule” section of this lesson.

  3. Which part of the Learn SAP curriculum covers Loops?

    • A. SAP installation
    • B. SAP Fiori
    • C. SAP ABAP development
    Show answer

    C. SAP ABAP development

    This lesson sits in the SAP ABAP development section of the Learn SAP course.

Frequently asked questions

What does the term Loops refer to in SAP?

Loops let ABAP process data repeatedly, most importantly iterating over internal tables. Writing loops correctly, and efficiently, is central to almost every ABAP program.

What is the performance impact of Loops?

The single most important ABAP performance rule involves loops: never put a database SELECT inside a loop over many rows.

What else is worth knowing about Loops?

Looping with ASSIGNING FIELD-SYMBOL avoids copying each row into a work area, faster and letting you modify the table row in place.

What tends to go wrong with Loops?

SELECT inside a loop, the top performance killer. Copying rows when a field-symbol would avoid it. Modifying a table while looping it incorrectly.
CallWhatsAppEnquire