SAP BOM Where-Used SQL Generator: STPO, MAST and STKO Queries for Components and Dependencies
A BOM where-used list answers a simple question: which parent products use this component? In SAP the answer sits across STPO, MAST and STKO. This generator writes the SQL for it, plus two helper queries for configurable BOMs.
Fill in
1 production, 2 engineering, 3 universal, 4 plant maintenance, 5 sales, 6 costing.
Where-used: parents of a component
SELECT m.matnr AS parent_material,
m.werks,
m.stlan AS bom_usage,
m.stlnr AS bom_number,
m.stlal AS alternative,
p.posnr AS item,
p.idnrk AS component,
p.menge AS quantity,
p.meins AS uom,
p.datuv AS valid_from
FROM stpo p
JOIN mast m
ON m.mandt = p.mandt
AND m.stlnr = p.stlnr
JOIN stko k
ON k.mandt = p.mandt
AND k.stlty = p.stlty
AND k.stlnr = p.stlnr
AND k.stlal = m.stlal
WHERE p.mandt = '100'
AND p.stlty = 'M'
AND p.idnrk = 'COMP-4410'
AND p.lkenz = ' '
AND m.werks = '1000'
AND m.stlan = '1'
ORDER BY m.matnr, m.stlal, p.posnr;Items of one parent that carry dependencies
SELECT m.matnr AS parent_material,
p.posnr AS item,
p.idnrk AS component,
p.knobj AS dependency_link,
p.datuv AS valid_from
FROM mast m
JOIN stpo p
ON p.mandt = m.mandt
AND p.stlnr = m.stlnr
WHERE m.mandt = '100'
AND m.matnr = 'CONF-PUMP-100'
AND m.werks = '1000'
AND m.stlan = '1'
AND p.stlty = 'M'
AND p.lkenz = ' '
AND p.knobj <> '0000000000'
ORDER BY p.posnr;Count BOM items per parent
SELECT m.matnr AS parent_material,
COUNT(*) AS item_count
FROM mast m
JOIN stpo p
ON p.mandt = m.mandt
AND p.stlnr = m.stlnr
WHERE m.mandt = '100'
AND m.werks = '1000'
AND m.stlan = '1'
AND p.stlty = 'M'
AND p.lkenz = ' '
GROUP BY m.matnr
ORDER BY item_count DESC;Verify on your system: material number length and padding (numeric materials are left padded with zeros in many releases), the KNOBJ empty value, and whether you need STAS to avoid duplicates on BOMs with item alternatives. These are read-only queries for reporting; for production support prefer the standard where-used transaction CS15.
Want where-used answers without writing SQL?
Ask about a read-only natural language assistant over your SAP BOM data.
Optional. The tool and every export work without this. No spam.
How to use the generator
Enter the client, the component, the plant and the BOM usage, and optionally a parent material for the dependency query. The first block returns every parent BOM that contains the component, with item number, quantity and valid-from date. The second block lists items of one parent that have dependency links, which is the quick way to see where selection conditions sit in a super BOM. The third block counts items per parent to find the biggest structures. Run it against a reporting copy or a read-only HANA view, not against the transactional application server.
How the BOM tables connect
MAST links a material and plant to a BOM number and alternative. STKO is the BOM header keyed by category, number and alternative. STPO holds the items, keyed by category, number, item node and counter. For a material BOM the category is M, so every query filters STLTY = M. Items marked for deletion carry a flag in LKENZ and should usually be excluded. The KNOBJ field on the item is the pointer to dependencies; a zero value means the item has none.
- MAST: material and plant to BOM number
- STKO: header and alternative
- STPO: items, quantities, dependency link
- Filter STLTY = M for material BOMs
Common mistakes
The usual mistake is trusting a simple STPO to MAST join on BOM number when alternatives and multiple counters exist; check the row counts against CS15 for a few components. Another is ignoring validity dates, so a component that was replaced still shows. Also remember that this is single level: a component can be used in a subassembly that is itself used elsewhere, so a full where-used needs a recursive query or CS15 with multilevel selected. And a generic SQL tool will not know your authorization setup, so confirm access with your security team.
Frequently Asked Questions
Is this a multilevel where-used?
No. It returns the direct parents of the component. For a multilevel result use CS15 with the multilevel option in SAP, or write a recursive query that repeats the join using each parent as the next component.
Why do I get duplicate rows?
Duplicates usually appear when a BOM has several alternatives or item counters and the join does not constrain them. Add the alternative condition through STKO as shown, or bring in STAS if your BOM uses item selection by alternative.
What does BOM usage mean?
BOM usage (STLAN) tells which area the BOM is for, for example 1 production, 2 engineering and design, 3 universal, 5 sales and 6 costing. It lets one material have different structures for different purposes. Choose the one your plant uses.
Can I run this on ECC?
Yes, the table structure is the same, but the data types differ, mainly the material number length. Adjust the values, test on a copy, and compare a few results with the standard transaction before you trust the query.
If planners keep asking where a component is used, Netray can set up a read-only assistant over your SAP BOM data that returns the answer with the SQL it ran.
Related Tools
SAP Variant Configuration Tables Reference
A searchable lookup of the database tables behind SAP characteristics, classes, object dependencies, variant tables and BOMs, with key fields and what each one holds.
SAPSAP BOM Category, Usage and Item Category Codes
A searchable lookup of the coded values consultants meet in BOM and classification work: BOM category, usage, item category, class type and dependency status.
SAPSAP Classification Values ABAP Generator
Enter a material and a class type and get an ABAP snippet that resolves the internal object number and lists every assigned characteristic and value.
SAPSAP Variant Configuration Combination Calculator
Enter the number of values for up to six characteristics and the share of valid combinations to see total configurations, test effort and a pairwise test lower bound.