ABAP SELECT Statement Generator
This generator produces the three SELECT patterns ABAP developers write every week: SELECT INTO TABLE with ORDER BY and UP TO n ROWS, SELECT SINGLE, and FOR ALL ENTRIES with the empty driver table guard. Change the table, field list and condition, then copy the code or export it.
Fill in
Transparent table, DDIC view or CDS view name.
Comma separated list, as required by the escaped host variable syntax.
Name of your local variable, without the @ sign.
Used to name lt_<suffix> and ls_<suffix>.
1. SELECT into an internal table
* Open SQL with inline declaration (ABAP 7.40 SP08 and later) SELECT vbeln, erdat, kunnr, netwr, waerk FROM vbak WHERE kunnr = @lv_kunnr ORDER BY erdat INTO TABLE @DATA(lt_orders) UP TO 100 ROWS. IF sy-subrc <> 0. * nothing found: handle the empty case explicitly ENDIF.
2. SELECT SINGLE
* Use only when the WHERE clause covers the full primary key, * otherwise use UP TO 1 ROWS with ORDER BY for a deterministic row. SELECT SINGLE vbeln, erdat, kunnr, netwr, waerk FROM vbak WHERE kunnr = @lv_kunnr INTO @DATA(ls_orders). IF sy-subrc = 0. * row found ENDIF.
3. FOR ALL ENTRIES (driver table must be checked)
* The driver table must contain the compared field and must not be empty.
* An empty driver table makes FOR ALL ENTRIES read the whole table.
IF lt_orders IS NOT INITIAL.
SELECT vbeln, erdat, kunnr, netwr, waerk
FROM vbak
FOR ALL ENTRIES IN @lt_orders
WHERE kunnr = @lt_orders-kunnr
INTO TABLE @DATA(lt_detail).
ENDIF.Inline declarations and @ host variable escaping need ABAP 7.40 SP05 or later, and the comma separated field list is required once you use the @ syntax. On older releases remove the @ signs and use INTO TABLE lt_x with a prior DATA declaration. FOR ALL ENTRIES removes duplicate result rows, so select the full key. Always test in a system with realistic volumes.
Skip the custom report
Tell us the data you keep extracting with ABAP and we will show how a read-only AI layer could answer it from your ERP data.
Optional. The tool and every export work without this. No spam.
How to use the generator
Enter the table or view name, the fields you actually need, one WHERE field and the name of the variable that holds its value. The result suffix controls names such as lt_orders and ls_orders. The output uses the modern Open SQL syntax with escaped host variables and inline declarations, so you do not need a separate DATA statement. Paste the block into your method or report, adjust the WHERE clause for extra conditions, and add a range table with IN @lr_range when users filter with SELECT-OPTIONS.
- Select only the fields you need, never SELECT * in production code
- Use ORDER BY only when order matters, it costs database time
- Use a range table for IN conditions from selection screens
SELECT SINGLE, UP TO 1 ROWS and FOR ALL ENTRIES
SELECT SINGLE is correct only when the WHERE clause specifies the complete primary key. If it does not, the row returned is not deterministic, and the ABAP test tools flag it. For a non-key condition use UP TO 1 ROWS with ORDER BY. FOR ALL ENTRIES is a join substitute for older code, it eliminates duplicate rows in the result and silently reads the whole table when the driver table is empty, which is why the generator wraps it in an IS NOT INITIAL check. On S/4HANA a real JOIN or a CDS view is usually faster and clearer.
Reading the result and handling sy-subrc
After SELECT INTO TABLE, sy-subrc is 0 when at least one row was read and 4 when none was found. sy-dbcnt holds the number of rows read. Always handle the empty case explicitly rather than looping over an empty table and hoping. When you SELECT into an inline declared table, remember that its structure follows the field list, so a later change in the SELECT changes the row type.
Common mistakes
The usual defects are SELECT inside a LOOP, missing client handling on cross-client tables, selecting without an index-supported WHERE clause, forgetting the sy-subrc check, and using FOR ALL ENTRIES with an empty driver table. Another is comparing fields of different types, which forces a conversion and can stop the database using an index. Check long running selects in transaction ST05 SQL trace and the runtime analysis in SAT.
Frequently Asked Questions
What is the difference between SELECT SINGLE and UP TO 1 ROWS?
SELECT SINGLE is meant for reads where the WHERE clause covers the full primary key. UP TO 1 ROWS returns the first row found after sorting, so pair it with ORDER BY when the condition is not a full key. For non-key conditions, UP TO 1 ROWS with ORDER BY is the deterministic choice.
Why does FOR ALL ENTRIES read the whole table when my internal table is empty?
When the driver table is empty, the FOR ALL ENTRIES condition is dropped and the remaining WHERE clause is applied, which can return everything. Always check the driver table with IS NOT INITIAL before the statement, as the generated code does.
Does this syntax work on ECC?
The escaped host variables and inline DATA(...) declarations need NetWeaver 7.40 SP05 or later, which covers ECC 6.0 EHP7 and above. On older releases, remove the @ signs, drop the commas in the field list, and declare the target table with a DATA statement first.
Should I still use FOR ALL ENTRIES on S/4HANA?
Often a JOIN or a CDS view is simpler and pushes the work to the HANA database, which is faster for large volumes. FOR ALL ENTRIES remains valid and is useful when the driver data comes from an API or earlier step, but test both on realistic data.
If your team writes the same ABAP extraction logic over and over, we can help build a read-only layer that answers those questions from your ERP data without custom reports.
Related Tools
ABAP Internal Table Declaration Generator
Choose table kind, key and line type and get the DATA declaration, a local TYPES version and the common table operations.
SAPABAP ALV Report Skeleton Generator
Enter a program name, table and selection field and get a working ABAP report that shows an ALV grid using CL_SALV_TABLE.
SAPABAP CDS View Skeleton Generator
Fill in table, fields, aliases and one association to get CDS view DDL in both the classic DDIC-based and the view entity syntax.
SAPABAP Code Review Checklist
A practical review checklist for ABAP changes: database access, security, error handling, structure and S/4HANA compatibility.
More SAP tools
See all- SAP Sales Document Types Reference
- SAP Pricing Procedure Design Worksheet
- SAP Item Category Determination Reference
- SAP Delivery Block Reasons Template
- SAP Credit Management Checklist
- SAP Output Determination Checklist
- SAP Billing Types Reference
- SAP Incompletion Log Checklist
- SAP SD Tables Reference (VBAK, VBAP, LIKP, VBRK and More)