Excellent question. This is a classic point of confusion for many ABAP developers, and understanding it is key to writing robust code.
Let's break it down.
Is sy-subrc = 4 OK for COUNT(*)?
No, not usually.
For a standard SELECT COUNT(*) statement, you should almost always expect sy-subrc = 0.
Here's why:
SELECT COUNT(*) is an aggregate function. Its job is to count the rows that match your WHERE clause.
- If rows are found, it returns the number (e.g., 5, 100, 1000). The operation was successful, so
sy-subrc = 0.
- If no rows are found that match your
WHERE clause, it returns the number 0. The database operation was still successful (it successfully determined the count is zero), so sy-subrc is still 0.
An sy-subrc of 4 from a SELECT statement means "no data found". With a standard COUNT(*), this shouldn't happen because the "data" you are selecting is the count itself, which is always returned.
The Most Likely Reason for sy-subrc = 4
The most common reason you would get sy-subrc = 4 from a SELECT statement that includes COUNT(*) is because you are also using a GROUP BY clause.
Consider this:
- Without
GROUP BY: The SELECT returns exactly one row containing the total count.
- With
GROUP BY: The SELECT returns one row for each group that it finds. If your WHERE clause filters out all records, then no groups can be formed, and the SELECT statement returns zero rows. In this case, sy-subrc will be 4.
Example:
" This will almost always return SY-SUBRC = 0
SELECT COUNT(*)
FROM sflight
INTO @DATA(lv_count)
WHERE carrid = 'XX'. " A carrier that doesn't exist
" After this select:
" lv_count will be 0
" sy-subrc will be 0 (because the query successfully returned a result: the number 0)
"--------------------------------------------------------------------
" This CAN return SY-SUBRC = 4
SELECT carrid, COUNT(*) AS flight_count
FROM sflight
INTO TABLE @DATA(lt_counts)
WHERE carrid = 'XX' " A carrier that doesn't exist
GROUP BY carrid.
" After this select:
" lt_counts will be an empty internal table
" sy-subrc will be 4 (because no rows were found to form any groups)
How a Proper SELECT COUNT Should Look
Here are the correct and recommended patterns.
Scenario 1: Getting the Total Count of Rows
This is the most common use case. You want to know how many records match a condition.
The Proper Way:
DATA lv_count TYPE i.
SELECT COUNT(*)
FROM sflight
INTO lv_count
WHERE carrid = 'LH'.
IF sy-subrc = 0.
WRITE: / |Total Lufthansa flights found: { lv_count }|.
ELSE.
" This block is for sy-subrc <> 0, which would indicate a
" database error, authorization issue, etc. (sy-subrc = 8)
WRITE: / |Error during database selection!|.
ENDIF.
Key Points:
- Select
COUNT(*) into a single integer-like variable.
- Always check
IF sy-subrc = 0. Even though 4 is unlikely, 8 (error) is possible.
Scenario 2: Just Checking if at least one Record Exists
If your goal is not to get the total count, but simply to check if any data exists, using SELECT COUNT(*) is inefficient. The database has to count everything, even if you only care about the result being > 0.
The Proper (and more performant) Way:
The best practice is to ask the database to find just one record and then stop.
" Modern ABAP 7.40+ Syntax (Recommended)
SELECT SINGLE @abap_true
FROM sflight
INTO @DATA(lv_exists)
WHERE carrid = 'LH'.
IF sy-subrc = 0.
" lv_exists now contains 'X' (abap_true)
WRITE: / |At least one Lufthansa flight exists.|.
ELSE.
" sy-subrc = 4 means NO record was found, which is a valid business outcome
WRITE: / |No Lufthansa flights exist.|.
ENDIF.
Alternative (older syntax, still valid):
SELECT SINGLE carrid
FROM sflight
INTO @DATA(lv_carrier)
WHERE carrid = 'LH'.
IF sy-subrc = 0.
WRITE: / |At least one Lufthansa flight exists.|.
ELSE.
WRITE: / |No Lufthansa flights exist.|.
ENDIF.
Why is this better?
The database optimizer understands that it can stop scanning the table as soon as it finds the first matching record. With COUNT(*), it must scan all matching records, which can be much slower on large tables.
Summary
| Your Goal | Recommended ABAP Statement | Expected sy-subrc | What sy-subrc = 4 Means |
|---|
| Get the total count of matching records. | SELECT COUNT(*) INTO @DATA(lv_count) ... | 0 | Unusual. Check for a GROUP BY clause or a rare DB issue. The count variable will be 0 if no records are found. |
| Check if any record exists (at least one). | SELECT SINGLE @abap_true INTO @DATA(lv_exists) ... | 0 or 4 | Normal. It means no matching record was found. This is often the "success" path in your logic (e.g., "OK to create a new record"). |
| Get a count for different groups. | SELECT field, COUNT(*) INTO TABLE ... GROUP BY field. | 0 or 4 | Normal. It means no records were found that could be grouped, so the resulting internal table is empty. |
Conclusion: If you are using a simple SELECT COUNT(*) without a GROUP BY and getting sy-subrc = 4, there is something very unusual happening. Double-check your code. If you are using GROUP BY, then sy-subrc = 4 is a normal and expected result when no data matches your WHERE clause.