ERPClimb logoERPClimb
Internal Tables
ABAP Developmentintermediate 8 min read

Internal Tables Part 5: SORT, BINARY SEARCH and Secondary Keys

Preview · a free account unlocks the full lesson

Key takeaway

Make key reads fast on standard tables with SORT and BINARY SEARCH, understand why the sort order must match, and use secondary keys when one table needs several access paths.

In this lessonShow sections

Visual overview

Internal Tables Part 5: SORT, BINARY SEARCH and Secondary Keys — learning sheetIn one line: Make key reads fast on standard tables with SORT and BINARY SEARCH, understand why the sort order must match, and use secondary keys when one table needs several access paths.ERPClimb.com / ABAP / LESSON 5/10 / LEARNING SHEETInternal Tables Part 5: SORT, BINARY SEARCH and SecondaryKeys1 · IN ONE LINEMake key reads fast on standard tables with SORT and BINARY SEARCH, understand why the sortorder must match, and use secondary keys when one table needs several access paths.2 · KEY IDEAS EXPLAINEDSortDefault is ascending; STABLE keeps the relativeorder of equal rows.Sorting costs time — do it once after filling.Binary SearchThe table must be sorted ascending by the samefields, in the same order, as the key used(leading fields).sy-subrc: 0 found; 4 not found (sy-tabix = insertposition); 8 not found and position beyond thelast row.Typical Pattern In Classic ReportsSELECT … INTO TABLE gt_carr.Why Not Always A Sorted Table?A sorted table keeps its order automatically andneeds no BINARY SEARCH addition — often cleaner.The SORT + BINARY SEARCH pattern is typical in…3 · DO · AVOIDBest practice!Common mistakeSort once by exactly the search key; keep SORTand BINARY SEARCH close together.Prefer sorted/hashed table types in new code.Use secondary keys for additional access pathson large tables.✕BINARY SEARCH on an unsorted or differentlysorted table.✕SORT inside loops.✕Searching with fields not in the leading sortorder.4 · IN A REAL PROJECTA developer added BINARY SEARCH to speed up a delivery report, but the lookup table was sorted by plant andmaterial while the READ used only material. The report ran fast — and silently reported "no stock"for 15% of materials that did have stock. The fix sorted the table by material (the search key) before…Original ERPClimb learning sheet • Verify against your SAP release and edition

Explanation

Part 4 covered reading rows by key, which on standard tables can be slow for large volumes. This part fixes that the classic way and the modern way, and shows a dangerous trap to avoid.

SORT puts the table in order, for example SORT gt_book BY carrid connid fldate. or SORT gt_book BY forcuram DESCENDING. The default order is ascending, and adding STABLE keeps the relative order of rows that are equal on the sort key. Sorting costs time, so do it once after filling the table rather than repeatedly.

Once the table is sorted, READ TABLE gt_book INTO gs_book WITH KEY carrid = 'LH' connid = '0400' BINARY SEARCH performs a fast logarithmic lookup instead of a linear scan.

For this to work correctly, the table must be sorted ascending by exactly the same fields, in the same order, as the key used in the read (the leading fields). sy-subrc comes back 0 when the row is found, 4 when it is not found (with sy-tabix giving the insert position), and 8 when it is not found and the position would be beyond the last row.

With non-unique keys, BINARY SEARCH finds the first matching row, which is useful as the starting point for a parallel cursor technique covered later. The real danger is that if the sort order does not match the key used, BINARY SEARCH can report "not found" for rows that actually exist — a silent bug that is easy to miss.

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.