Open SQL
ABAP Developmentadvanced

Open SQL Part 9: HANA-Friendly SQL and Code Pushdown

Key takeaway

Adapt classic SQL habits for SAP HANA: fewer round trips, aggregation and calculation in the database, SQL expressions (CASE, CAST, string/date functions), subqueries, and when to move logic into CDS views.

Explanation

📌 KEY POINTS
✅ On HANA the biggest wins are fewer round trips and less data transferred — not micro-optimizing ABAP.

✅ Push work to the database: filtering, joins, aggregation, calculations with SQL expressions.

✅ Classic rules still apply: no SELECT in loops, field lists instead of *, FAE checks.

💡 Logic reused by several programs belongs in a CDS view.

━━━━━━━━━━━━━━━━━━━━
▶ WHERE WE ARE

Parts 1–8 taught correct and measured SQL. After a move to S/4HANA or Suite on HANA, the same code often runs faster — but classic patterns that move data to ABAP waste most of HANA's strength.

━━━━━━━━━━━━━━━━━━━━
▶ 1. WHAT CHANGES WITH HANA

  • Column store: reading few columns of many rows is very fast; SELECT * of wide tables is costly.
  • In-memory aggregation and joins are very fast.
  • Many secondary indexes of the old database are unnecessary; some statements need a different approach.
  • The expensive part is the round trip and transfer between application server and database.

━━━━━━━━━━━━━━━━━━━━
▶ 2. CODE PUSHDOWN IN ABAP SQL (7.40 SP05+ STRICT SYNTAX)

  • Calculations: price * seatsocc AS revenue.
  • CASE: CASE WHEN seatsocc >= seatsmax THEN 'FULL' ELSE 'OPEN' END AS status.
  • CAST, COALESCE, string functions (CONCAT, SUBSTRING, UPPER), date functions (DATS_DAYS_BETWEEN), arithmetic functions (ROUND, DIV).
  • Aggregations with GROUP BY/HAVING (Part 6).
  • Subqueries: WHERE EXISTS ( SELECT … ), IN ( SELECT … ).
  • Common table expressions (WITH) in newer releases.

━━━━━━━━━━━━━━━━━━━━
▶ 3. CLASSIC ANTI-PATTERNS TO REMOVE

  • SELECT … ENDSELECT with processing per row → INTO TABLE.
  • SELECT inside LOOP → JOIN/FAE + internal table.
  • Nested SELECTs → JOIN.
  • Reading everything and filtering/aggregating in ABAP → WHERE/GROUP BY.
  • Unnecessary sorting in ABAP of sorted DB results.

━━━━━━━━━━━━━━━━━━━━
▶ 4. WHEN TO USE CDS VIEWS

If the same join/calculation is needed in several programs, services or analytics, model it once as a CDS view entity (see RAP/CDS module) and select from it. For HANA-specific procedural logic, AMDP exists — but only as an exception.

━━━━━━━━━━━━━━━━━━━━
▶ 5. WHAT STAYS IN ABAP

Complex business rules, calls to BAPIs/APIs, authorization checks, messages, and UI — pushdown is for data-intensive work, not for all logic.

━━━━━━━━━━━━━━━━━━━━
▶ STEP BY STEP

Compare the classic and pushed-down versions in the code example with ST05: statements, rows transferred, runtime.

✅ CHECKPOINT: you move data-intensive work to the database and keep business logic readable in ABAP.

Code example

ABAP Code
REPORT zosql_part9.* Note: SQL expressions (CASE, CAST, arithmetic, SQL functions) exist only in the strict* Open SQL syntax (7.40 SP08+ / 7.50). Classic syntax has no equivalent - on older* releases these calculations stay in the ABAP loop shown as CLASSIC below. PARAMETERS p_carr TYPE s_carr_id DEFAULT 'LH'. START-OF-SELECTION.* ===== CLASSIC (moves data to ABAP, computes there) =====* SELECT * FROM sflight INTO TABLE gt_all WHERE carrid = p_carr.* LOOP AT gt_all INTO gs.*   lv_rev = gs-price * gs-seatsocc.*   IF gs-seatsocc >= gs-seatsmax. lv_status = 'FULL'. ELSE. lv_status = 'OPEN'. ENDIF.*   ...* ENDLOOP. * ===== PUSHDOWN: calculate, classify and filter in the database =====  SELECT carrid,         connid,         fldate,         currency,         CAST( price AS DEC( 15, 2 ) ) * seatsocc          AS revenue,         CASE WHEN seatsocc >= seatsmax THEN 'FULL'              WHEN seatsocc >= seatsmax * 9 / 10 THEN 'ALMOST'              ELSE 'OPEN' END                             AS status,         dats_days_between( @sy-datum, fldate )           AS days_to_departure    FROM sflight    WHERE carrid = @p_carr      AND fldate >= @sy-datum    ORDER BY fldate    INTO TABLE @DATA(lt_flights). * ===== Subquery: connections that have at least one cancelled booking =====  SELECT carrid, connid, cityfrom, cityto    FROM spfli AS p    WHERE carrid = @p_carr      AND EXISTS ( SELECT bookid FROM sbook                     WHERE carrid = p~carrid                       AND connid = p~connid                       AND cancelled = 'X' )    INTO TABLE @DATA(lt_conn_with_cancellations). * ===== Aggregation instead of COLLECT in ABAP =====  SELECT carrid, currency, SUM( price * seatsocc ) AS revenue    FROM sflight    WHERE fldate BETWEEN '20260101' AND '20261231'    GROUP BY carrid, currency    INTO TABLE @DATA(lt_revenue). * Reused by several programs/services? -> model it as a CDS view entity.

Real project scenario

After a migration to S/4HANA, a margin report was only slightly faster than before. ST05 showed it read 3 million document lines with SELECT * and computed margins, statuses and totals in ABAP loops.
Rewriting the core as one SELECT with CASE expressions, arithmetic and GROUP BY — later moved into a CDS view reused by an analytical app — reduced runtime from 25 minutes to 40 seconds and removed 600 lines of ABAP.

Common mistakes

  • Assuming HANA makes any code fast automatically.
  • SELECT * on wide tables in column store.
  • Computing and aggregating in ABAP what SQL can do.
  • Pushing complex business rules into unreadable SQL.
  • Copy-pasting the same join/calculation into many programs instead of a CDS view.

Best practices

  • Minimize round trips and transferred data.
  • Use SQL expressions, aggregation and subqueries in strict syntax.
  • Remove SELECT-in-loop and nested SELECT patterns.
  • Model reusable logic as CDS views.
  • Keep business rules and API calls in readable ABAP.

Interview angle

Questions: "What is code pushdown?", "What changes for ABAP SQL on HANA?", "Which SQL features support pushdown?"
Strong answer: code pushdown moves data-intensive operations (filter, join, aggregate, calculate) to the database to reduce transfer and round trips; on HANA column store and in-memory processing make this very effective while SELECT * and row-by-row processing waste it; ABAP SQL supports CASE, CAST, arithmetic, string/date functions, subqueries and aggregates; reusable logic goes to CDS views; business logic stays in ABAP.

Practice this lesson

Quick check

1. Which strict-syntax feature lets the database classify rows?

2. Where should a join-and-calculation used by several programs and services be modeled?

3. Which change is a typical code pushdown on HANA?

Interview questions

1. Explain the concept of database pushdown in S/4HANA and how it influences your Open SQL design decisions.

Answer it in your own words first, then compare.

2. Which classic SQL habits hurt most after moving to SAP HANA, and how do you fix them?

Answer it in your own words first, then compare.

Practice more with Interview AI →

Reference-verified: the steps are checked against official SAP documentation and tutorials, and object and field names in the sample code are checked for consistency. The code has not been compiled or run on an SAP system by ERPClimb. Syntax marked release-dependent varies by SAP release, so check it in your own system.

More lessons in this topic

Interview questions on this topic

Related SAP topics