SAP examples · LessonBy Anitha M, SAP Trainer, 13 yrs · Published · Updated · SAP S/4HANA 2023 · all levels
SAP SQL examples
These SQL examples show querying SAP data effectively, whether Open SQL in ABAP or native HANA SQL, with an emphasis on efficient, set-based access.
Quick answer
Write set-based SQL that filters (WHERE), joins and aggregates in the database, returning only what you need.
Key takeaways
- Watch out: SELECT * / no WHERE, reading too much.
- Worked examples: open sql in abap (modern syntax), joins and aggregation, what it teaches.
- How SQL examples fits into SAP development and the wider SAP landscape
Open SQL in ABAP (modern syntax)
SELECT ebeln, SUM( netwr ) AS total
FROM ekpo
WHERE werks = @lv_plant
GROUP BY ebeln
INTO TABLE @DATA(lt_po_totals).Joins and aggregation
SELECT k~ebeln, k~lifnr, SUM( p~netwr ) AS total
FROM ekko AS k INNER JOIN ekpo AS p ON k~ebeln = p~ebeln
WHERE k~bsart = 'NB'
GROUP BY k~ebeln, k~lifnr
INTO TABLE @DATA(lt).What it teaches
Write set-based SQL that filters (WHERE), joins and aggregates in the database, returning only what you need. This is efficient on any database and essential on HANA (code-to-data). Avoid SELECT *, always use a WHERE, and never loop the database.
Common pitfalls
- SELECT * / no WHERE, reading too much.
- Aggregating in ABAP instead of SQL.
- SELECT in a loop.
Practice challenge
+0 XPStreak ×0
Question 1 of 3
Which statement is true of SQL examples?