ERPClimb logoERPClimb
Open SQL
ABAP Developmentbeginner 8 min read

Open SQL Part 4: SELECT SINGLE vs UP TO 1 ROWS

Preview · a free account unlocks the full lesson

Key takeaway

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 sections

Visual overview

Open SQL Part 4: SELECT SINGLE vs UP TO 1 ROWS — learning sheetIn one line: 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.ERPClimb.com / ABAP / LESSON 4/10 / LEARNING SHEETOpen SQL Part 4: SELECT SINGLE vs UP TO 1 ROWS1 · IN ONE LINERead exactly one row the right way: SELECT SINGLE with the full key, UP TO 1 ROWS with ORDERBY for "the first/latest" row, existence checks, buffering effects and the ATCwarnings you will see.2 · KEY IDEAS EXPLAINEDSelect SingleComplete primary key in WHERE → exactly one row ornone.Uses the table buffer for buffered tables when thefull key is specified.Incomplete Key — The ProblemSELECT SINGLE * FROM sflight INTO gs_flight WHEREcarrid = 'LH'.Up To 1 Rows + Order ByDefined result: the latest flight.Existence ChecksSELECT SINGLE @abap_true FROM sbook INTO@DATA(lv_exists) WHERE customid = @lv_cust.(7.40+)Classic: SELECT SINGLE customid FROM sbook INTOlv_cust WHERE customid = lv_cust. — select onesmall field.3 · DO · AVOIDBest practice!Common mistakeSELECT SINGLE only with the full primary key.UP TO 1 ROWS + ORDER BY for defined singlerows.Minimal field lists for existence checks.✕SELECT SINGLE with incomplete keys expecting"the first" row.✕Reading entire rows for existence checks.✕SELECT SINGLE inside loops on non-bufferedtables.4 · IN A REAL PROJECTA pricing program read "the current price" with SELECT SINGLE on a condition table using onlymaterial and customer, without the validity date. On one database the returned row happened to be the newest;after the HANA migration, a different (older) row came back, and invoices were priced wrongly for two days…Original ERPClimb learning sheet • Verify against your SAP release and edition

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.

Free account

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

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.