Treat a lookup as a result that can fail
Looking up an ID is not the same as guaranteeing that the row exists. A dispatch can be missing, deleted, or outside the current work set. READ TABLE makes the lookup explicit and sy-subrc tells you whether a row was found.
Check before using the target
READ TABLE dispatches
WITH TABLE KEY dispatch_id = 'D-1102'
INTO DATA(found).
IF sy-subrc = 0.
DATA(result) = found-status.
ENDIF.
The target is meaningful only after a successful read. Keep the sy-subrc check adjacent to the statement so later changes do not accidentally use a failed lookup’s initial target.
Use the table’s declared key when it matches the lookup. A free-key search is possible, but the cost and behavior depend on the table category and key. For a lookup that needs a distinct fallback, consider a table expression with OPTIONAL or DEFAULT instead.
The sample is a method-body fragment; READ TABLE does not access a database. ABAP Cloud and classic systems share these core concepts, though language-version and release availability should be checked for advanced additions.
Sources
- SAP Samples: Internal Tables — reading lines using table keys
- ABAP Keyword Documentation: READ TABLE
Developer practice · ABAP
Practice: check whether a dispatch was found
Read dispatch D-1102 from a uniquely keyed table and set result to the status only when the read succeeds.
This choice changes the output pattern checked here: WRITE belongs in a classic executable report, while out->write( ) uses the supplied output reference in an ABAP Cloud class-run method. It does not switch the language version of the other code; check release and API availability in your target system.
Complete the task and include the output statement shown for your selected environment. Variable names can differ if you use them consistently. This browser checker checks structure only; it does not compile or run ABAP.
0 required structures detected · 0 checks
Write your solution, then check its structure.
One possible solution
TYPES: BEGIN OF ty_dispatch,
dispatch_id TYPE string,
status TYPE string,
END OF ty_dispatch.
TYPES ty_dispatches TYPE HASHED TABLE OF ty_dispatch WITH UNIQUE KEY dispatch_id.
DATA(dispatches) = VALUE ty_dispatches( ( dispatch_id = 'D-1102' status = 'READY' ) ).
READ TABLE dispatches WITH TABLE KEY dispatch_id = 'D-1102' INTO DATA(found).
IF sy-subrc = 0.
DATA(result) = found-status.
ENDIF.
Compare another approach
Use a table expression with OPTIONAL
This concise form returns an initial value when the key is missing, so use it only when that fallback is acceptable. READ TABLE plus sy-subrc is clearer when the caller must distinguish “not found” from a row whose status is initial.
DATA(status) = VALUE string(
dispatches[ dispatch_id = 'D-1102' ]-status OPTIONAL ).
WRITE result.out->write( result ).These are context-specific output examples. In your own program, use the result variable from your solution and the output mechanism available in that program.
Debug this code
Assume dispatches is a keyed internal table. A lookup can fail; repair this snippet so found-status is read only after a successful read.
Edit the code, then check for the expected repair. This is a structure check, not an ABAP compiler.
Find the issue, repair it, and check your change.
Diagnosis and repair
Diagnosis: The code reads found-status without checking whether READ TABLE succeeded. Check sy-subrc immediately after the read and only then use found.
READ TABLE dispatches WITH TABLE KEY dispatch_id = 'D-1102' INTO DATA(found).
IF sy-subrc = 0.
DATA(result) = found-status.
ENDIF.