SAP examples · LessonReviewed by Anitha M, SAP Trainer, 13 yrs · Updated · Published · 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
Two Open SQL examples in modern syntax: a SELECT over EKPO summing NETWR per purchase order for one plant with GROUP BY into an inline table, and an inner join of EKKO to EKPO filtered on document type NB and aggregated per order and vendor. The lesson is set-based SQL that filters, joins and aggregates in the database.
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.