ERPClimb logoERPClimb
Internal Tables
ABAP Developmentbeginner 9 min read

Internal Tables Part 6: APPEND, INSERT, MODIFY, DELETE and COLLECT

Preview · a free account unlocks the full lesson

Key takeaway

Change internal tables correctly: APPEND vs INSERT for each table kind, MODIFY by index or with WHERE, DELETE single rows, ranges and WHERE conditions, COLLECT for totals, and APPEND LINES for whole tables.

In this lessonShow sections

Visual overview

Internal Tables Part 6: APPEND, INSERT, MODIFY, DELETE and COLLECT — learning sheetIn one line: Change internal tables correctly: APPEND vs INSERT for each table kind, MODIFY by index or with WHERE, DELETE single rows, ranges and WHERE conditions, COLLECT for totals, and APPEND LINES for whole tables.ERPClimb.com / ABAP / LESSON 6/10 / LEARNING SHEETInternal Tables Part 6: APPEND, INSERT, MODIFY, DELETE andCOLLECT1 · IN ONE LINEChange internal tables correctly: APPEND vs INSERT for each table kind, MODIFY by index orwith WHERE, DELETE single rows, ranges and WHERE conditions, COLLECT for totals, and APPENDLINES for whole tables.2 · KEY IDEAS EXPLAINEDAdding RowsAPPEND gs_flight TO gt_flight. — standard tables(and sorted if the order stays correct).INSERT gs_flight INTO TABLE gt_flight. — generic:standard → end, sorted → correct position, hashed→ by key. With unique keys, a duplicate is not…Changing RowsIn a loop with field symbols (Part 3) — preferred.MODIFY gt_flight FROM gs_flight INDEX lv_tabix. —one row by index.Deleting RowsDELETE gt_flight INDEX 3.DELETE TABLE gt_flight FROM gs_flight. — by tablekey.Collect — TotalsCOLLECT gs_total INTO gt_total.3 · DO · AVOIDBest practice!Common mistakeINSERT … INTO TABLE for generic, key-safeinserts.Mass statements (MODIFY/DELETE … WHERE)instead of loops.COLLECT only with sorted/hashed tables, oraggregate in SQL.✕APPEND to sorted tables out of order (runtimeerror).✕Deleting rows inside a loop over the sametable.✕Row-by-row MODIFY where MODIFY … WHEREsuffices.4 · IN A REAL PROJECTA data cleansing program deleted invalid records one by one inside a LOOP over a table of 3 million rows andused the loop index for later MODIFY statements. Runtime was over two hours, and after each deletion thestored indexes pointed to the wrong rows, corrupting some records. Replacing the loop with DELETE … WHERE and…Original ERPClimb learning sheet • Verify against your SAP release and edition

Explanation

Parts 1 through 5 covered reading internal tables; now it's time to change them. To add rows, APPEND gs_flight TO gt_flight adds a row at the end, which works for standard tables (and sorted tables as long as the resulting order stays correct).

INSERT gs_flight INTO TABLE gt_flight is more generic: for a standard table it adds at the end, for a sorted table it finds the correct position, and for a hashed table it inserts by key — and with unique keys, a duplicate simply isn't inserted, leaving sy-subrc at 4.

INSERT gs_flight INTO gt_flight INDEX 1 inserts at a specific position in standard or sorted tables, APPEND LINES OF gt_other TO gt_flight appends an entire other table at once, and the modern functional style gt_flight = VALUE #( BASE gt_flight ( ... ) ) achieves the same result inline.

To change rows, the preferred approach is usually a loop using field symbols, as covered in Part 3. Where that doesn't fit, MODIFY gt_flight FROM gs_flight INDEX lv_tabix changes a single row by its index, MODIFY TABLE gt_flight FROM gs_flight changes a row located by its table key (for sorted or hashed tables), and MODIFY gt_flight FROM gs_upd TRANSPORTING price WHERE carrid = 'LH' changes many matching rows at once, touching only the listed fields.

Free account

Create a free account to keep reading

You have read the opening of this lesson. A free ERPClimb account unlocks the rest of it, every other lesson and topic, and your learning history, so you can pick up exactly where you stopped. No card needed.

  • Full explanation
  • Code example
  • Real project scenario
  • Common mistakes
  • Best practices
  • Interview angle

One tap with Google works too. Every topic keeps its first lesson open to everyone.

Keep exploring

Check before use: lesson examples are educational, not a guarantee of correctness for your SAP edition or release. Sample code has not been executed on your SAP system. Confirm API availability, syntax, authorizations and business impact in an approved development or test environment.