ERPClimb logoERPClimb

Code examples · Modern ABAP · Intermediate

How much stock do we have? Add up MARD per plant and show the base unit from MARA

The problem

A warehouse planner wants one line per material for plant 1000 with the total unrestricted-use stock over all storage locations and the unit it is measured in.

How we solve it

  1. 1Read stockMARD per plant
  2. 2Add the unitjoin MARA
  3. 3Showloop

The result

The five materials with the most unrestricted-use stock in plant 1000, with total and unit.

Overview drawn by ERPClimb from this example. The steps are the same as in the code below.

1. The requirement

A warehouse planner wants one line per material for plant 1000 with the total unrestricted-use stock over all storage locations and the unit it is measured in. Materials with no stock must not appear.

2. How we design it

Stock is kept per material, plant and storage location in table MARD (field LABST is unrestricted-use stock). The planner wants it per material, so the database adds the storage locations together with SUM and GROUP BY. The unit is a property of the material, not of the stock row, so we join MARA for the base unit MEINS. HAVING removes materials whose total is zero, and the result is limited and sorted so that the biggest stocks come first.

Why we decided it this way

  1. 1Add up in the databaseA material can sit in many storage locations. The database returns one row per material, not hundreds of stock rows.
  2. 2Join MARA for the unitThe quantity is meaningless without its unit. MEINS is stored once per material in MARA.
  3. 3The plant in the WHEREWithout it the query reads the stock of every plant. Always limit by plant when you can.

Before you start

  • A system with stock. Look at it first in transaction MMBE (stock overview) for a material, and in SE16N in tables MARD and MARA.
  • ADT and a package such as $TMP.

3. The code, step by step

Step 1 of 3: Add up the stock per material

One row per material with the total over the storage locations.

ABAP · Class · ZCL_STOCK_LIST (main method, part 1)
" SUM( labst ) with GROUP BY and HAVING
CLASS zcl_stock_list DEFINITION PUBLIC FINAL CREATE PUBLIC.
  PUBLIC SECTION.
    INTERFACES if_oo_adt_classrun.
ENDCLASS.

CLASS zcl_stock_list IMPLEMENTATION.
  METHOD if_oo_adt_classrun~main.
    SELECT s~matnr,
           SUM( s~labst ) AS stock,
Part by part (plain text)
  1. SUM( labst ) with GROUP BY and HAVING. MARD has one row per material, plant and storage location. SUM( s~labst ) adds the storage locations of a material together. The plant is in the WHERE, so only plant 1000 is read. HAVING SUM( s~labst ) > 0 removes materials without stock after the sum is known.

Open in the portal

The SAP objects named in this code are linked in the code above and listed here. Each opens its page in the SAP Library.

SAP tables

4. The final outcome

The five materials with the most unrestricted-use stock in plant 1000, with total and unit.

What you should see

Example output (your system has other data): FG-100 1250.000 ST FG-200 980.000 ST RM-300 420.000 KG

Try it and check the result

  • Put the three SELECT parts together in order, activate with Ctrl+F3 and run with F9.
  • Take the first material that is printed and open transaction MMBE: the unrestricted-use total of plant 1000 matches.
  • Change the plant to one that has no stock: the program says that no material has stock.

Common mistakes

  • Stock has other categories: quality inspection (INSME), blocked (SPEME) and in transfer. LABST is only the unrestricted-use stock. Say which one you report.
  • Special stocks (for example sales order stock, vendor consignment) are in other tables. MARD does not hold all stock of a material.
  • In S/4HANA the movement history moved to table MATDOC, and the way current stock is read differs by release. For a new app prefer a released stock CDS view from the SAP API hub, and check SAP's notes for your release.

Keep learning

Checking status: Reviewed line by line against SAP documentation. Not executed on a live system. Reviewed 2026-11-25. Release: SAP S/4HANA or ECC system (for example an S/4HANA trial appliance from SAP Cloud Appliance Library). Uses SAP business tables, which are not released for ABAP Cloud.

Original ERPClimb teaching code with illustrative names. Not taken from a customer system. Syntax can differ by release; check the references below and activate in a sandbox first. ERPClimb is independent of SAP SE.