Workik.

SAP S/4HANA Custom Code Migration: An ABAP Developer's Guide

ABAP on SAP HANA: Sorting, Database Hints and Code Pushdown

Optimize ABAP database access for SAP HANA by enforcing explicit sorting, removing legacy database hints, and applying modern Open SQL code pushdown.

Moving your custom code to SAP S/4HANA involves more than fixing syntax errors and redirected tables. The underlying database engine changes completely. In SAP ERP Central Component (SAP ECC), your code ran on classic row-based relational databases where the database interface and table architectures masked certain coding habits. On SAP HANA, the database processes columns in parallel in memory. Code that worked for years by pure coincidence can suddenly return unpredictable data or drop in performance.

Previous lesson: S/4HANA Data Model Changes: MATNR, KONV, VBUK and MATDOC | Next lesson: Clean Core and ABAP Cloud for Migrated Systems


The Hidden Trap: Implicit Sorting and Missing ORDER BY

On classic any-DB platforms, running a SELECT statement without an ORDER BY clause often returned records in primary key order. Developers wrote code relying on this behavior, immediately running a binary search on the resulting internal table.

SAP HANA is a column store database that processes queries across multiple CPU cores simultaneously. When you query a table without an explicit sort order, the order of returned rows is completely undefined. One execution might return rows by key, while the next returns them in an arbitrary sequence depending on thread execution.

If your code executes a READ TABLE ... BINARY SEARCH on an internal table that was not explicitly sorted, the binary search algorithm fails silently. It skips valid records or returns the wrong row because binary search requires strictly sorted data.

Common Mistake: Assuming that a SELECT into an internal table preserves database index sequence. Always add an explicit ORDER BY in your query or run an explicit SORT statement on the internal table before any binary search.

Here is how you fix this pattern in ABAP:

abap
Copy
abap
Copy

Alternatively, if you cannot alter the query sorting, sort the internal table in ABAP memory before searching:

abap
Copy

What Happened to Pool and Cluster Tables

In classic SAP systems, pool tables (such as A004) and cluster tables (such as BSEG and BSEC) stored many logical rows packed inside physical database containers. The database interface handled unpacking and decompression in the application server layer.

Because the database interface unpacked cluster records by their primary key, it always sorted the returned dataset implicitly. You never needed an ORDER BY clause when reading BSEG on an older database because the database interface delivered it sorted by MANDT, BUKRS, BELNR, GJAHR, and BUZEI.

During the migration to SAP S/4HANA, all pool and cluster tables are transformed into standard transparent database tables. That intermediate unpacking layer no longer exists. Queries against BSEG hit transparent database storage directly.

Because these tables are now regular transparent tables on SAP HANA, that guaranteed sorting vanishes. Queries reading financial document lines or condition records that omit ORDER BY will return records in no guaranteed order. Any downstream logic expecting line items in sequence (BUZEI 001, 002, 003) can produce wrong totals or broken output.

Pro Tip: Code Inspector and ATC include checks that find SELECT statements without ORDER BY whose results your code later treats as sorted. Run these checks across your financial and pricing custom programs to locate these hidden dependencies before testing starts.


Database Hints, Native SQL and Harbor Supply Co.

Harbor Supply Co. has a custom pricing export program, ZSD_PRICE_EXPORT. When the original developers faced performance bottlenecks on their old database ten years ago, they wrote database hints and direct database calls.

Database hints are comments injected into Open SQL (Structured Query Language) statements that instruct the database cost-based optimizer to use a specific index. Native SQL statements bypass Open SQL entirely using EXEC SQL or ABAP Database Connectivity (ADBC).

These historical optimizations cause two issues during migration:

  1. Database hints written for third-party databases (like Oracle or Microsoft SQL Server) are ignored by SAP HANA. They no longer help performance and only clutter the code, so remove them.
  2. Native SQL bypasses the compatibility views and table redirects that Open SQL uses. A native SQL query that reads KONV will not see the pricing data, because S/4HANA stores it in PRCD_ELEMENTS.

Look at the original implementation in Harbor Supply Co.'s ZSD_PRICE_EXPORT:

abap
Copy

In SAP S/4HANA, condition records reside in PRCD_ELEMENTS, and optimizer hints written for older engines must be removed. The replacement code uses modern Open SQL targeted at the new storage model:

abap
Copy

Always check each field you select against PRCD_ELEMENTS in your system because pricing field names match closely but should be confirmed.


Code Pushdown: Moving Logic into the Database

In classic ABAP programming, developers learned to fetch raw data rows from the database and perform aggregation, calculations, and filtering inside LOOP AT blocks in the application server. This model minimized load on traditional disk-based database processors.

On SAP HANA, the paradigm shifts to code pushdown. You push data-intensive calculations, filtering, and groupings down into the in-memory database engine. The database computes sums, maximums, and string transformations at high speed, returning only the final result to the ABAP application server.

You can think of code pushdown like an office assistant: instead of asking them to bring you ten thousand filing folders so you can count how many invoices were paid, you ask them to count the paid invoices in the archive room and bring back just the final number.

Consider a reporting requirement in Harbor Supply Co.'s sales dashboard that calculates total order quantities per material for a specific date range.

The old ABAP approach retrieved thousands of line items into application memory:

abap
Copy

With code pushdown in modern Open SQL, the database aggregates the lines and returns only the summarized rows:

abap
Copy

For advanced reuse scenarios, you can define Core Data Services (CDS) views that encapsulate joins and business calculations directly in the database data dictionary. ABAP programs then read the CDS view just like a transparent table.


Verifying SQL with ATC and Runtime Tools

Static checks and dynamic runtime checks help you find database-related issues across thousands of custom objects.

ABAP Test Cockpit (ATC) runs static code analysis against the S4HANA_READINESS check variant. ATC catches missing ORDER BY statements after pool and cluster conversions, flags native SQL statements that need evaluation, and spots obsolete database hints.

However, static checks cannot predict every runtime data problem. Transaction SRTCM (Runtime Check Monitor) monitors live program executions in test or sandbox systems. It identifies queries that return unordered results that downstream code reads without sorting.

Review this checklist when evaluating database access in custom code:

  • Identify all custom SELECT statements hitting cluster tables like BSEG and verify that sort order is either explicitly requested or handled before reading.
  • Scan for BINARY SEARCH additions on internal tables and verify the table is sorted by the exact search keys beforehand.
  • Eliminate database hints designed for third-party databases.
  • Convert EXEC SQL and ADBC calls to standard Open SQL so they benefit from table redirections and compatibility views.
  • Replace application loops that aggregate data with SQL aggregate expressions (SUM, COUNT, AVG, MIN, MAX) and GROUP BY clauses.

Try It Yourself

Examine the following custom ABAP code snippet taken from an inventory reporting utility. It contains two problems for an SAP S/4HANA migration on SAP HANA: an implicit sort dependency and an unnecessary application-side aggregation loop.

abap
Copy

Rewrite this code to resolve both issues:

  1. Add explicit sorting to ensure the binary search works reliably on SAP HANA.
  2. Replace the procedural calculation loop with an Open SQL aggregate expression that calculates total stock in the database.

Sample solution:

abap
Copy

In the next lesson, Clean Core and ABAP Cloud for Migrated Systems, you will explore the four clean core levels, learn how ABAP Cloud ensures upgrade-stable development, and see how to position your custom code for the future.

Was this lesson helpful?