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
- 1Read stockMARD per plant
- 2Add the unitjoin MARA
- 3Showloop
The result
The five materials with the most unrestricted-use stock in plant 1000, with total and unit.
SAP objects in play
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
- 1Add up in the databaseA material can sit in many storage locations. The database returns one row per material, not hundreds of stock rows.
- 2Join MARA for the unitThe quantity is meaningless without its unit. MEINS is stored once per material in MARA.
- 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.
" 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)
- 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.
Function modules
If it does not work
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.