Sub topic 17
How material data is stored in tables
This chapter assumes you know the material master views and organizational levels from earlier chapters: client, plant, storage location, sales area and valuation area. It also assumes you can open a table in transaction SE16N or SE11. The ABAP examples need only basic reading skills.
One table per organizational level
The material master looks like a single record in transactions such as MM01 and MM03. In the database it is split into many tables. The split follows the organizational levels: each table holds the data that is valid at one level, and its key contains the organizational units for that level.
| Table | Level | Key fields | Typical content |
|---|---|---|---|
| MARA | Client | MATNR | Material type (MTART), industry sector (MBRSH), base unit (MEINS), material group (MATKL), deletion flag (LVORM) |
| MAKT | Client, per language | MATNR, SPRAS | Short description (MAKTX) |
| MARM | Client, per unit | MATNR, MEINH | Alternative units of measure and conversion factors |
| MARC | Plant | MATNR, WERKS | MRP type (DISMM), procurement type (BESKZ), purchasing group (EKGRP), plant status (MMSTA) |
| MARD | Storage location | MATNR, WERKS, LGORT | Storage-location data such as storage bin (LGPBE) |
| MVKE | Sales area | MATNR, VKORG, VTWEG | Sales data, such as the delivering plant and the material pricing group |
| MBEW | Valuation area | MATNR, BWKEY, BWTAR | Price control (VPRSV), moving average price (VERPR), standard price (STPRS), price unit (PEINH) |
The general rule is that data valid for the whole company is stored once, in MARA. Data that differs per plant is stored once per plant, in MARC. A material extended to three plants has one MARA row and three MARC rows.
Relationships between the tables
A few details are easy to miss:
- A MARD row always needs a MARC row for the same material and plant. You cannot create storage-location data for a plant the material is not extended to.
- MBEW is joined to MARC through the valuation area. The field is called BWKEY. If valuation is at plant level, which is the usual setting and the only one in S/4HANA, BWKEY equals the plant number WERKS. Do not assume this in generic code, but you can rely on it in S/4HANA.
- BWTAR is the valuation type. It is blank unless split valuation is active for the material. A blank value gives the "normal" valuation row. Forgetting this field in a join is a common cause of duplicate rows.
- MAKT has one row per language. If you join it without a condition on SPRAS, each material appears once per maintained language.
Reading the tables with ABAP SQL
The joins always start from MARA or MARC and connect on the key fields. This example lists stock-relevant master data for one plant. It joins MARC, MARD and MBEW, and it takes the valuation row without split valuation.
SELECT marc~matnr,
marc~werks,
mard~lgort,
mbew~vprsv,
mbew~verpr,
mbew~stprs,
mbew~peinh
FROM marc
INNER JOIN mard ON mard~matnr = marc~matnr
AND mard~werks = marc~werks
LEFT OUTER JOIN mbew ON mbew~matnr = marc~matnr
AND mbew~bwkey = marc~werks
AND mbew~bwtar = ''
WHERE marc~werks = '1010'
INTO TABLE @DATA(lt_valuation)
UP TO 50 ROWS.
IF sy-subrc = 0.
cl_demo_output=>display( lt_valuation ).
ENDIF.The LEFT OUTER JOIN keeps materials that have no valuation row, for example because they were extended to the plant but not yet to the accounting view. With an INNER JOIN those materials would silently disappear from the result.
Always filter on the plant or another key field. MARA and MARC can hold millions of rows, and a join without a WHERE condition on the key reads far more data than you need.
Which field is in which table
When you look for a field behind a screen, use these steps:
- Open the material in MM03, place the cursor in the field and press F1. Choose Technical Information. The dialog shows the table name and field name.
- Open the table in SE11 to see the key and data element.
- Check the data in SE16N with the material number as selection.
Some data is not in the main tables:
- Long texts, such as the purchase order text, are stored as text objects, not as table fields. Read them with function module
READ_TEXT. - Classification data (the Classification view) is in the classification tables, such as AUSP and KSSK, not in MARA.
- Change history is in the change document tables CDHDR and CDPOS, with object class MATERIAL.
Material numbers and conversions
MATNR is the key of every table above. In S/4HANA the field is 40 characters long. Earlier releases used 18. The field has a conversion exit, MATN1. It affects numeric numbers: on screen the user types 4711, but the database stores 000000000000004711, padded with leading zeros. A select with matnr = '4711' therefore finds nothing. Either pass the number through CONVERSION_EXIT_MATN1_INPUT or use the internal format. Alphanumeric numbers such as ZDEMO_MAT_001 are stored as typed.
What changed in S/4HANA
The master data tables keep their names and structure. The changes affect what sits next to them:
- Stock quantities. In earlier releases, quantities were stored as totals in MARC, MARD and related tables (such as the history tables MARCH and MARDH). In S/4HANA, material documents are stored in the table MATDOC, and the old aggregate tables are replaced by compatibility views that calculate the quantities from MATDOC. Read stock quantities through the compatibility views or the released CDS views, and do not write to the old tables.
- Valuation. Material ledger is always active. Price data remains in MBEW, but valuation per currency and per type is also kept in material ledger tables. Valuation at plant level is the only option.
- Business partner and number length. The 40-character material number is the change that most often breaks custom code. Check structures and interfaces that hold MATNR in 18-character fields.
For custom code that reads or changes material data, the SAP custom code migration check shows which statements touch changed tables.
Writing to the tables
Do not insert or update MARA, MARC or MBEW directly. A direct write skips authorization checks, number assignment, field status checks and change documents, and it can leave tables inconsistent. Use BAPI_MATERIAL_SAVEDATA for changes. It fills the correct tables and requires an X structure (such as BAPI_MARCX) that marks which fields you intend to change. A field that is set in the data structure but not flagged in the X structure is ignored. It is a frequent error with this BAPI, and it returns no error message.
Common errors
- Selecting on a numeric material number without the conversion exit, and getting no rows.
- Joining MAKT without
spras = @sy-langu, and getting duplicate rows. - Joining MBEW without
bwtar, and getting duplicate rows for split-valuated materials. - Using an inner join to MBEW or MARD and losing materials that are not extended to those levels.
- Reading stock quantities directly from old aggregate tables in S/4HANA custom code.
Exercise
A material is extended to plants 1010 and 1020, and plant 1010 has two storage locations. It has descriptions in English and German. How many rows does it have in MARA, MAKT, MARC and MARD, assuming MARD has no rows for plant 1020 and valuation is at plant level without split valuation? How many rows does MBEW have?
Show answer
MARA has 1 row. MAKT has 2 rows, one per language. MARC has 2 rows, one per plant. MARD has 2 rows, both for plant 1010. MBEW has 2 rows, one per valuation area, which equals one per plant, and BWTAR is blank in both.