Open SQL Part 4: SELECT SINGLE vs UP TO 1 ROWS
Preview · a free account unlocks the full lesson
Read exactly one row the right way: SELECT SINGLE with the full key, UP TO 1 ROWS with ORDER BY for "the first/latest" row, existence checks, buffering effects and the ATC warnings you will see.
In this lessonShow sectionsHide sections
Visual overview
Explanation
Parts 1 through 3 covered reading sets of rows; very often, though, you just need one row — the airline's name, a particular customer, the latest booking — and that's the focus here. SELECT SINGLE is the right choice when you specify the complete primary key, since that guarantees exactly one unique row.
UP TO 1 ROWS combined with ORDER BY is the right choice when several rows could match and you want a specific, well-defined one — the latest, or the cheapest. One trap worth avoiding: SELECT SINGLE with an incomplete key returns some arbitrary matching row, not reliably "the first" one.
And for a plain existence check, select just one small field, or even a constant, rather than reading the whole row.
A statement like SELECT SINGLE carrname FROM scarr INTO lv_name WHERE carrid = p_carr. works cleanly because the WHERE clause supplies the complete primary key, guaranteeing exactly one row or none at all. It also benefits from the table buffer, for buffered tables, precisely because the full key is specified. sy-subrc comes back 0 when found, 4 when not.
The trouble starts with an incomplete key: SELECT SINGLE * FROM sflight INTO gs_flight WHERE carrid = 'LH'. will match many rows, and the database simply returns one of them, arbitrarily. Code that assumes it's getting "the first flight" is really just working by coincidence, and the Code Inspector or ATC will warn about exactly this pattern — SELECT SINGLE without a full key.
Create a free account to keep reading
You have read the opening of this lesson. A free ERPClimb account unlocks the rest of it, every other lesson and topic, and your learning history, so you can pick up exactly where you stopped. No card needed.
- Full explanation
- Code example
- Real project scenario
- Common mistakes
- Best practices
- Interview angle
One tap with Google works too. Every topic keeps its first lesson open to everyone.
Keep exploring
Interview questions on this topic
Related SAP topics
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.