Sub topic 17
How material data is stored in tables
This chapter assumes you know what organizational levels are (client, plant, storage location, valuation area) and can read basic ABAP SQL. It shows how the material master's levels and views map onto database tables.
The core tables
Each organizational level has its own table. A material's data is spread across these tables, and the key of each table shows the level it belongs to.
| Table | Level | Key | Typical content |
|---|---|---|---|
| MARA | Client | MATNR | Material type, base unit of measure, material group |
| MAKT | Client, per language | MATNR, SPRAS | Material description |
| MARC | Plant | MATNR, WERKS | MRP type, purchasing group, procurement type |
| MARD | Storage location | MATNR, WERKS, LGORT | Stock quantities per storage location |
| MBEW | Valuation area | MATNR, BWKEY, BWTAR | Price control, standard price, moving average price |
Sales data sits in MVKE, at the level of sales organization and distribution channel. It is not in the list above, but it follows the same pattern.
Views are not tables
A view such as Purchasing or MRP 1 is a screen grouping. It is not a table. The Purchasing view shows fields from MARA and from MARC on one screen. Which table holds a field depends on its organizational level, not on the view that displays it.
This is why extending a material to a new plant adds a row to MARC, and usually rows to MARD and MBEW. It does not copy the MARA row.
Reading across tables
To list materials for one plant, join the tables on MATNR. The example below returns the material type from MARA, the description from MAKT, and the MRP type from MARC.
SELECT mara~matnr,
makt~maktx,
mara~mtart,
marc~werks,
marc~dismm
FROM mara
INNER JOIN makt ON makt~matnr = mara~matnr
AND makt~spras = @sy-langu
INNER JOIN marc ON marc~matnr = mara~matnr
WHERE marc~werks = '1010'
INTO TABLE @DATA(lt_materials)
UP TO 100 ROWS.The condition on makt~spras matters. Without it, a material with descriptions in three languages returns three rows.
Common errors
- Missing rows after an inner join. A material that has no description in the logon language disappears from the result. Use a
LEFT OUTER JOINon MAKT if you need every material. - Duplicate rows. Joining MARD without restricting storage location returns one row per storage location.
- Reading MBEW with the wrong key. The valuation area (BWKEY) is usually the plant, but it can be the company code. Check the valuation level in customizing before you join on
werks. - Writing directly to the tables. Never update MARA, MARC, or the other tables with SQL. Standard checks, change documents, and dependent data are skipped. Use a BAPI such as
BAPI_MATERIAL_SAVEDATA, covered later in this tutorial.
Exercise
A material is extended to plant 1010 and plant 1020, and each plant has two storage locations. How many MARA, MARC, and MARD rows exist for this material?
Show answer
One MARA row, because general data is stored once per client. Two MARC rows, one per plant. Four MARD rows, one per plant and storage location combination.
Exercise
Why can a query that joins MARA with MAKT return several rows for a single material, and how do you prevent it?
Show answer
MAKT holds one row per language for each material. Add a condition on the language field, for example makt~spras = @sy-langu, to the join.