Batch Genealogy SQL Query Generator (Forward and Backward Trace)
When a supplier raises a quality alert or a lot fails stability, quality has to know which finished batches used the affected material and where they went. This generator writes SQL for the forward and backward trace over SAP material documents.
Fill in
Forward trace: orders that consumed this batch
-- Component batch B240915A of material 000000000010000123 issued to orders (movement 261, not reversed)
SELECT s.AUFNR AS order_no,
s.MBLNR AS mat_doc,
k.BUDAT AS posting_date,
s.WERKS AS plant,
s.MENGE AS qty,
s.MEINS AS uom
FROM SAPSR3.MSEG s
JOIN SAPSR3.MKPF k
ON k.MANDT = s.MANDT AND k.MBLNR = s.MBLNR AND k.MJAHR = s.MJAHR
WHERE s.MANDT = '100'
AND s.WERKS = '1000'
AND s.MATNR = '000000000010000123'
AND s.CHARG = 'B240915A'
AND s.BWART = '261'
AND k.BUDAT >= '2025-01-01'
ORDER BY k.BUDAT, s.MBLNR;Backward trace: components consumed to make this batch
-- Finished batch B240915A received from an order (movement 101), then the components that order consumed
SELECT r.AUFNR AS order_no,
c.MATNR AS component,
c.CHARG AS component_batch,
c.MENGE AS qty,
c.MEINS AS uom
FROM SAPSR3.MSEG r
JOIN SAPSR3.MSEG c
ON c.MANDT = r.MANDT AND c.AUFNR = r.AUFNR AND c.BWART = '261'
WHERE r.MANDT = '100'
AND r.WERKS = '1000'
AND r.MATNR = '000000000010000123'
AND r.CHARG = 'B240915A'
AND r.BWART = '101'
AND r.AUFNR <> ''
ORDER BY r.AUFNR, c.MATNR;Reversals to review
-- Reversals (262) for the same batch, to net against issues above SELECT s.AUFNR AS order_no, s.MBLNR AS mat_doc, s.MENGE AS qty FROM SAPSR3.MSEG s WHERE s.MANDT = '100' AND s.WERKS = '1000' AND s.MATNR = '000000000010000123' AND s.CHARG = 'B240915A' AND s.BWART = '262';
Run on a read-only replica or extract, never on a production database during peak load. SAP stores dates as YYYYMMDD character fields on many systems, so adjust the date literal to your database. In S/4HANA, material documents may be served by MATDOC, so verify table names. Reversal handling and multi-level trace through intermediates need extra joins.
Plain-language batch tracing inside your network
An optional review of a read-only trace assistant that shows its SQL and respects your SAP roles.
Optional. The tool and every export work without this. No spam.
How to use the generator
Enter the plant, material number, batch, a start date and the schema that holds your SAP tables. The page fills three queries: a forward trace, a backward trace and a reversal check. Copy them into your SQL client against a read-only copy of the data. Remember that SAP material numbers are padded to 18 characters in many systems, so copy the value from MM03 or MB51 rather than typing it.
How the trace works
SAP records every goods movement in MSEG with the material, batch, movement type and, for order related movements, the order number in AUFNR. Movement 261 is a goods issue of a component into an order, and movement 101 is the goods receipt of the finished batch from that order. Joining the two movements through the order number links components to finished goods without any custom table.
Reading the output
The forward query returns each order that consumed the batch, with the date and quantity. Take the order numbers into the backward logic to find the finished batches, then follow those batches to customers through movement 601 deliveries. If an order shows both 261 and 262 for the same batch, net the quantities before concluding how much was used.
- Check every plant that could have received the batch.
- Include reversals and scrap postings in the quantity balance.
- Compare the result with MB56 as a sanity check.
Common mistakes
The most common error is skipping reversals, which overstates consumption. Another is tracing only one level, when a batch of an intermediate was made from the affected raw material. Finally, be careful with client filters and date formats, and never run a heavy trace against a live production database in the middle of a shift.
Frequently Asked Questions
Does this replace MB56?
No. MB56 is the standard transaction and is a good first check. The SQL is useful for bulk traces, exports to Excel and recall exercises where you need many batches at once or want to join the result to delivery and customer data.
Will it work on S/4HANA?
The logic is the same, but S/4HANA uses MATDOC for material documents and keeps MSEG and MKPF as compatibility views. Table names, field lengths and date formats can differ, so test the query in a non-production system first.
How do I trace to customers?
Take the finished batches from the backward step and look for movement 601 goods issues or delivery items with the same batch. Delivery tables such as LIPS hold batch and delivery number, and the ship-to comes from the delivery header.
Is this safe to run on production?
Run it against a read-only replica or extract if possible. The queries are SELECT only, but unindexed scans of MSEG can be heavy on large systems. Add a plant and date filter, and ask your Basis or DBA team about indexes.
If you need multi-level traces across plants, Netray can build a private on-prem tracing assistant that runs read-only queries over SAP, shows the SQL it used and respects ERP authorisations.
Related Tools
SAP Batch Management GMP T-Codes and Tables Reference
A lookup of SAP transactions, tables and movement types for batch genealogy, inspection and release.
Developer ToolsGxP Audit Trail Entry JSON Generator
Build a structured audit trail record with user, time, change, reason and signature meaning for a regulated integration.
Compliance & ContractsPharma Data Integrity ALCOA+ Audit Checklist
Walk through ALCOA+ expectations for paper records, lab instruments and electronic systems in a regulated plant.
Compliance & ContractsPharma GMP Inspection Readiness Assessment
Score how ready your site is for a regulatory GMP inspection and see where to focus first.
More Developer Tools tools
See all- SAP Sales Order ABAP SELECT Generator (VBAK, VBAP, VBFA)
- SAP Sales Order Create BAPI ABAP Generator (BAPI_SALESORDER_CREATEFROMDAT2)
- SAP ABAP Open Items SELECT Generator
- SAP ACDOCA SQL Query Generator
- SAP QM Inspection Lot ABAP SELECT Generator
- SAP Quality Notification Pareto SQL Query Generator
- SAP QM Usage Decision BAPI Skeleton
- ABAP Custom Exception Class Generator
- ABAP Unit Test Class Generator