# ABAP SELECT Statement Generator

> Source: https://netray.co/tools/abap-select-statement-generator
> Publisher: Netray (netray.co), AI and integration engineering for ERP and manufacturing systems.
> Section: Free Tools

## Short answer

Fill in table, fields and a WHERE condition and get modern ABAP Open SQL: SELECT INTO TABLE, SELECT SINGLE and a safe FOR ALL ENTRIES pattern.

## Category

sap

## Erp

SAP ECC / S/4HANA

## Audience

- ABAP developer
- SAP technical consultant

## Tool

**Kind:** template

**Output:** code

**Language:** abap

**Fields:**

#### Database table or view

**Key:** table

**Type:** text

**Default:** vbak

**Help:** Transparent table, DDIC view or CDS view name.

#### Fields (comma separated)

**Key:** fields

**Type:** text

**Default:** vbeln, erdat, kunnr, netwr, waerk

**Help:** Comma separated list, as required by the escaped host variable syntax.

#### WHERE field

**Key:** where_field

**Type:** text

**Default:** kunnr

#### WHERE host variable

**Key:** where_value

**Type:** text

**Default:** lv_kunnr

**Help:** Name of your local variable, without the @ sign.

#### ORDER BY field

**Key:** order_field

**Type:** text

**Default:** erdat

#### Result name suffix

**Key:** alias

**Type:** text

**Default:** orders

**Help:** Used to name lt_<suffix> and ls_<suffix>.

#### UP TO n ROWS

**Key:** rows

**Type:** number

**Default:** 100

**Blocks:**

#### 1. SELECT into an internal table

**Body:** * Open SQL with inline declaration (ABAP 7.40 SP08 and later)
SELECT {{fields}}
  FROM {{table}}
  WHERE {{where_field}} = @{{where_value}}
  ORDER BY {{order_field}}
  INTO TABLE @DATA(lt_{{alias}})
  UP TO {{rows}} ROWS.
IF sy-subrc <> 0.
* nothing found: handle the empty case explicitly
ENDIF.

#### 2. SELECT SINGLE

**Body:** * 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 {{fields}}
  FROM {{table}}
  WHERE {{where_field}} = @{{where_value}}
  INTO @DATA(ls_{{alias}}).
IF sy-subrc = 0.
* row found
ENDIF.

#### 3. FOR ALL ENTRIES (driver table must be checked)

**Body:** * 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_{{alias}} IS NOT INITIAL.
  SELECT {{fields}}
    FROM {{table}}
    FOR ALL ENTRIES IN @lt_{{alias}}
    WHERE {{where_field}} = @lt_{{alias}}-{{where_field}}
    INTO TABLE @DATA(lt_detail).
ENDIF.

**Notes:** 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.

## Content

**Intro:** 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.

**Sections:**

#### How to use the generator

**Body:** 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.

**Bullets:**

- 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

**Body:** 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

**Body:** 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

**Body:** 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.

**Cta:** 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.

**Faq:**

#### What is the difference between SELECT SINGLE and UP TO 1 ROWS?

**Answer:** 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?

**Answer:** 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?

**Answer:** 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?

**Answer:** 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.

## Search terms

abap select statement example, abap select into table, abap select single, abap for all entries example, abap open sql generator, abap inline declaration select, abap select where in range

---
Talk to an engineer: https://netray.co/contact?intent=erp_ai | Machine catalog: https://netray.co/agent/catalog.json
