Open SQL Part 1: How ABAP Reads the Database
Free sample lesson · open to everyone
Understand Open SQL (ABAP SQL): database-independent access to dictionary tables, client handling, the SELECT forms you will meet in classic code, and the classic vs new strict syntax.
In this lessonShow sectionsHide sections
Visual overview
Expert visual walkthrough
How ABAP Reads the Database
Open SQL · Chapter-specific technical teaching map
Integration path
Application filter
Open SQL statement
Database engine
Result table
Tutor's chapter takeaway
Understand Open SQL (ABAP SQL): database-independent access to dictionary tables, client handling, the SELECT forms you will meet in classic code, and the classic vs new strict syntax.
In a real project
A custom report used native SQL (EXEC SQL) "for speed" to read an SAP table. After a system copy into a multi-client test system, the report showed data from all clients, including a training client with fictitious amounts…
Watch out for
• Using native SQL in application code. • Writing MANDT into WHERE conditions unnecessarily. • Mixing classic and strict syntax in one statement. • SELECT … ENDSELECT for large result sets. • Ignoring sy-subrc after SELECT.
The situation: a real project scenario
A custom report used native SQL (EXEC SQL) "for speed" to read an SAP table. After a system copy into a multi-client test system, the report showed data from all clients, including a training client with fictitious amounts, because native SQL does not add the client condition.
Rewriting it in Open SQL restored automatic client handling, allowed table buffering and made the report database-independent for the planned HANA migration.
Explanation
Open SQL — now officially called ABAP SQL — is ABAP's database-independent way of reading dictionary tables and views: you write SQL in ABAP, and the database interface translates it into the SQL dialect of whatever database actually sits underneath (HANA, Oracle, DB2, and so on), so your program works unchanged across every supported database.
This lesson is part of a ten-part series: Part 1 (this lesson) covers how ABAP reads the database; Part 2 covers SELECT basics — field lists, WHERE, INTO; Part 3 covers FOR ALL ENTRIES; Part 4 compares SELECT SINGLE with UP TO 1 ROWS; Part 5 covers JOINs; Part 6 covers aggregates, GROUP BY, and HAVING; Part 7 covers PACKAGE SIZE and large data; Part 8 covers performance analysis with ST05, SAT, and SQL Monitor; Part 9 covers HANA-friendly SQL and code pushdown; and Part 10 covers Open SQL in real reports and interfaces. The running example throughout uses SCARR, SPFLI, SFLIGHT, SBOOK, and SCUSTOM.
The client (MANDT) is handled automatically: client-dependent tables carry MANDT as their first key field, and Open SQL adds "MANDT = sy-mandt" to the query on your behalf, so you never write it yourself in the WHERE clause. CLIENT SPECIFIED (the classic syntax) and USING CLIENT (the newer syntax) exist only for rare cross-client tools.
Tables that are flagged for buffering in SE11 — typically small customizing tables — are read from the application server's buffer, which is very fast. Certain statements bypass that buffer regardless, including aggregates, JOINs, explicit BYPASSING BUFFER, and certain WHERE conditions.
You'll encounter several forms of SELECT: SELECT SINGLE … INTO wa WHERE <full key> reads exactly one row; SELECT … INTO TABLE itab WHERE … reads many rows into a table at once and is the preferred form; SELECT … INTO wa … ENDSELECT loops over rows one at a time using the classic syntax, and is usually slower; and SELECT … FOR ALL ENTRIES IN itab performs driver-table-based reads, covered in Part 3.
There are also two syntax styles. Classic syntax looks like SELECT carrid connid FROM sflight INTO TABLE gt_flight WHERE carrid = p_carr, with space-separated fields and no @ sign.
Strict syntax, available from release 7.40 SP05 onward, looks like SELECT carrid, connid FROM sflight WHERE carrid = @p_carr INTO TABLE @gt_flight, and follows a comma-separated field list, uses @ to mark ABAP variables, and recommends placing INTO at the end.
You cannot mix the two styles within a single statement, though both remain valid in classic systems, and the newer syntax is what enables inline declarations and SQL expressions.
Finally, sy-subrc tells you whether rows were found (0) or not (4), and sy-dbcnt tells you how many rows were affected. To practice, run the code example that performs the same selection in both classic and strict syntax, compares SELECT … ENDSELECT against INTO TABLE, and checks sy-dbcnt. By the end, you should be able to read any Open SQL statement, whether written in classic or new syntax.
Code example
REPORT zosql_part1. DATA: gt_flight TYPE STANDARD TABLE OF sflight, gs_flight TYPE sflight, gs_carr TYPE scarr, gv_lines TYPE i. PARAMETERS p_carr TYPE s_carr_id DEFAULT 'LH'. START-OF-SELECTION.* ===== 1. SELECT ... INTO TABLE: all rows in one round trip ===== SELECT * FROM sflight INTO TABLE gt_flight WHERE carrid = p_carr. " MANDT added automatically DESCRIBE TABLE gt_flight LINES gv_lines. WRITE: / 'INTO TABLE, rows:', gv_lines, 'sy-dbcnt:', sy-dbcnt. " sy-dbcnt = rows the DB actually returned * ===== 2. SELECT ... ENDSELECT: row by row (avoid for large volumes) ===== SELECT * FROM sflight INTO gs_flight UP TO 5 ROWS WHERE carrid = p_carr. WRITE: / gs_flight-connid, gs_flight-fldate. ENDSELECT. " one database round trip PER ROW -> fine for a handful, costly at scale * ===== 3. SELECT SINGLE with full key ===== SELECT SINGLE * FROM scarr INTO gs_carr WHERE carrid = p_carr. " SCARR's key is MANDT + CARRID, so this is a full-key read -> at most one row IF sy-subrc = 0. WRITE: / 'airline:', gs_carr-carrname. ENDIF. * NEVER in application code:* EXEC SQL. SELECT ... FROM SFLIGHT ... ENDEXEC. -> no client handling, no portability * 💡 Modern (strict) syntax, 7.40 SP05+: commas in field lists, @ for host variables,* INTO at the end and inline targets:* SELECT carrid, connid, fldate FROM sflight WHERE carrid = @p_carr* INTO TABLE @DATA(lt_flights).* Classic and strict syntax cannot be mixed within one statement.Watch out
Common mistakes
- Using native SQL in application code.
- Writing MANDT into WHERE conditions unnecessarily.
- Mixing classic and strict syntax in one statement.
- SELECT … ENDSELECT for large result sets.
- Ignoring sy-subrc after SELECT.
Do this
Best practices
- Use Open SQL for all application database access.
- Prefer INTO TABLE over SELECT … ENDSELECT.
- Use strict syntax in new code where the release allows.
- Let Open SQL handle the client.
- Check sy-subrc / sy-dbcnt.
In the interview
Interview angle
Questions: "What is Open SQL?", "How is the client handled?", "Classic vs new SQL syntax?"
Strong answer: Open SQL/ABAP SQL is the database-independent SQL layer for dictionary objects; the client is added automatically; buffering applies to buffered tables; classic syntax uses space-separated fields without @, strict syntax uses commas, @ host variables and allows expressions and inline declarations; avoid native SQL in application code.
Practice this lesson
Quick check
0 of 3 questions attempted
Question 1 of 3
Which statement about Open SQL client handling is correct?
Question 2 of 3
Best practice for large result sets in ABAP is to:
Question 3 of 3
Which is the recommended replacement for SELECT inside LOOP?
Practise this in an interview
Answer in your own words and get instant feedback, the way an interviewer would judge it.
1What is the impact of client handling in Open SQL and how do you deal with cross-client queries safely?
2Describe how client handling (MANDT) works in Open SQL. When is client filtering automatic, and when must it be handled explicitly?
Try a related Code Lab
This published exercise covers the same SAP topic. Apply the ideas in a hands-on scenario, then return to review your answers.
Open: FOR ALL ENTRIES with an empty driver table →See it in code
Each example starts from a requirement, shows the design, then the code step by step with a plain-language explanation of every part.
Was this lesson helpful?
Keep exploring
Interview questions on this topic
Related SAP topics
Referenced SAP objects
Reviewed reference pages for the objects this page mentions.
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.