# SAP Open Production Orders ABAP SELECT Statement Generator

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

## Short answer

Fill in plant, order type and dates and get a ready ABAP Open SQL query for open production orders.

## Category

sap

## Erp

SAP S/4HANA

## Audience

- ABAP developer
- SAP PP consultant
- Data analyst

## Tool

**Kind:** template

**Output:** code

**Language:** abap

**Fields:**

#### Plant

**Key:** plant

**Type:** text

**Default:** 1000

**Help:** Four character plant code (WERKS).

#### Order type

**Key:** order_type

**Type:** text

**Default:** PP01

**Help:** AUART of the production order type.

#### Scheduled start from (YYYYMMDD)

**Key:** date_from

**Type:** text

**Default:** 20260901

#### Scheduled start to (YYYYMMDD)

**Key:** date_to

**Type:** text

**Default:** 20261231

#### Maximum rows

**Key:** max_rows

**Type:** number

**Default:** 500

**Blocks:**

#### Open production orders in a plant

**Body:** DATA: lv_werks TYPE werks_d VALUE '{{plant}}',
      lv_auart TYPE aufart VALUE '{{order_type}}',
      lv_from  TYPE d VALUE '{{date_from}}',
      lv_to    TYPE d VALUE '{{date_to}}'.

SELECT aufk~aufnr,
       aufk~auart,
       afpo~matnr,
       afpo~psmng,
       afpo~wemng,
       afpo~meins,
       afko~gstrp,
       afko~gltrp
  FROM aufk
  INNER JOIN afko ON afko~aufnr = aufk~aufnr
  INNER JOIN afpo ON afpo~aufnr = aufk~aufnr
  WHERE aufk~werks = @lv_werks
    AND aufk~auart = @lv_auart
    AND afko~gstrp BETWEEN @lv_from AND @lv_to
    AND NOT EXISTS ( SELECT objnr FROM jest
                       WHERE objnr = aufk~objnr
                         AND inact = ''
                         AND stat IN ('I0045','I0046','I0076') )
  ORDER BY afko~gstrp, aufk~aufnr
  INTO TABLE @DATA(lt_orders)
  UP TO {{max_rows}} ROWS.

cl_demo_output=>display( lt_orders ).

#### Late orders: past due and not delivered

**Body:** SELECT aufk~aufnr,
       afpo~matnr,
       afpo~psmng,
       afpo~wemng,
       afko~gltrp
  FROM aufk
  INNER JOIN afko ON afko~aufnr = aufk~aufnr
  INNER JOIN afpo ON afpo~aufnr = aufk~aufnr
  WHERE aufk~werks = @lv_werks
    AND afko~gltrp < @sy-datum
    AND afpo~wemng < afpo~psmng
    AND NOT EXISTS ( SELECT objnr FROM jest
                       WHERE objnr = aufk~objnr
                         AND inact = ''
                         AND stat IN ('I0045','I0046','I0076') )
  ORDER BY afko~gltrp
  INTO TABLE @DATA(lt_late)
  UP TO {{max_rows}} ROWS.

#### Status texts used above

**Body:** * I0045 = TECO (technically completed)
* I0046 = CLSD (closed)
* I0076 = DLFL (deletion flag)
* Add I0012 (DLV, delivered) if you also want to hide fully delivered orders.

**Notes:** Written for ABAP 7.4 and later Open SQL with host variables (@). On older releases remove the @ escapes and use the older INTO syntax. Verify field names in SE11, test in a sandbox and add authority checks (AUTHORITY-CHECK for object C_AUFK_ATY or your own) before using in a production report. A report reading all plants at once can be slow; keep the plant and date filters.

## Content

**Intro:** A list of open production orders is the first report most plants ask for and the most frequently rewritten. This generator produces the Open SQL for it, using the real SAP tables, with a variant for late orders.

**Sections:**

#### How to use the generator

**Body:** Enter the plant, order type and scheduled start window. The code on the right updates immediately. Copy it into a report in SE38 or an ADT project. The first block lists orders still open, the second lists orders whose scheduled finish is past and whose delivered quantity is below the planned quantity. Replace cl_demo_output with an ALV if the list goes to users.

#### Why the status filter uses JEST

**Body:** Order status is not a field on AFKO or AFPO. It lives in JEST, keyed by the object number AUFK-OBJNR, with one row per status and an INACT flag for statuses that were once set and later reversed. Filtering for TECO, CLSD and deletion flag (I0045, I0046, I0076) with INACT blank gives the orders that are still genuinely open. A NOT EXISTS subquery avoids duplicate rows that a plain join would cause.

**Bullets:**

- Date columns GSTRP and GLTRP are scheduled start and finish.
- PSMNG is planned quantity, WEMNG delivered quantity.
- Add JEST status I0002 (REL) in a positive EXISTS test to list only released orders.

#### Mistakes to avoid

**Body:** Do not join AFKO to AFPO without understanding that multi-item orders (co-products) have more than one AFPO row; the join repeats header data. Do not forget the plant filter, because AUFK holds all order categories, including maintenance and internal orders, so also filter on order category if you only want production orders. Finally, check that the report is scoped before running in production, since tables like AFPO are large.

**Cta:** Netray builds read-only query layers over SAP so planners can ask for late orders and shortages in plain language, without custom ABAP for each report. Tell us your landscape and we will scope it.

**Faq:**

#### Why does the query exclude status I0045?

**Answer:** I0045 is TECO, technically completed. Orders in TECO are no longer open for execution, even if they were not fully delivered. The query hides TECO, closed and deletion-flagged orders to leave the work that still needs attention.

#### Can I run this on ECC as well as S/4HANA?

**Answer:** The tables are the same, but the syntax with @ host variables and inline declarations needs ABAP 7.4 or later. For older systems, remove the @ symbols, declare the result table yourself and use the older INTO TABLE form.

#### How do I list only released orders?

**Answer:** Add a positive EXISTS subquery on JEST for status I0002 with INACT blank. Combine it with the not exists test on TECO and closed. The status I0002 means released (REL) in the standard status schema.

#### Does this query handle process orders?

**Answer:** Process orders from PP-PI use the same order tables with a different order category, so the join works, but you filter on the right order type and category. Components, phases and recipe data come from other structures, so extend the query accordingly.

## Search terms

sap open production orders abap, abap select afko afpo, sap production order query, abap jest status not exists, sap late production orders report, abap production order open sql, sap aufk afko join, sap teco status i0045

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