Sub topic 11
Where organisational units live in the database
This chapter assumes you know the hierarchy from the earlier chapters: client, company code, plant and storage location. It also assumes you can read basic relational tables and Open SQL. Here you will see which database tables hold each unit. You will also see how the assignments between units are stored, and how to query them.
Two kinds of tables: structure and data
SAP stores organisational units in two groups of tables.
- Customizing tables define that a unit exists and how it is linked to other units. Examples are
T001(company codes),T001W(plants) andT001L(storage locations). They hold few rows and change rarely. They are normally maintained through customizing transactions and transported between systems. - Application data tables hold business data at the level of an organisational unit. Examples are
MARA,MARCandMARDfor materials and stock. They hold millions of rows. Their key fields refer to the customizing tables.
When you read an organisational unit's key in an application table, such as WERKS in MARD, it is a reference to a row in a customizing table. Open SQL does not enforce that reference as a database foreign key. The check is done by the application and by the field's check table in the ABAP Dictionary.
The main tables at a glance
| Unit | Table | Key fields | Useful non-key fields |
|---|---|---|---|
| Client | `T000` | `MANDT` | `MTEXT` (name), `ORT01` (city), `CCCATEGORY` (client role) |
| Company code | `T001` | `MANDT`, `BUKRS` | `BUTXT` (name), `ORT01`, `LAND1`, `WAERS` (currency), `KTOPL` (chart of accounts) |
| Plant | `T001W` | `MANDT`, `WERKS` | `NAME1`, `BWKEY` (valuation area), `LAND1`, `REGIO`, `ORT01` |
| Valuation area, with company code assignment | `T001K` | `MANDT`, `BWKEY` | `BUKRS` |
| Storage location | `T001L` | `MANDT`, `WERKS`, `LGORT` | `LGOBE` (description) |
| Company | `T880` | `MANDT`, `RCOMP` | `NAME1`, `LAND1`, `CURR` |
Three details in this table matter in practice.
T000hasMANDTas its key but is client-independent. It lists all clients of the system, so it does not need a client column to filter on.T001Lhas a two-part key.LGORTalone is not unique, which is the database form of "a storage location is unique only within its plant".T001Kholds the link between valuation area and company code. This is why the plant-to-company-code assignment does not appear as a column inT001W.
Client handling in Open SQL
All the tables except T000 have MANDT as the first key field. In Open SQL you normally never mention MANDT. The runtime adds a condition for the current logon client automatically. This is called automatic client handling.
This is why the queries in this chapter show no MANDT condition and still return data from only one client. To read across clients you must write CLIENT SPECIFIED explicitly. Doing so is a deliberate decision, and in most business code it is wrong.
Plants and their company codes
The plant row does not store a company code. The path goes through the valuation area:
T001W-BWKEYpoints to the valuation area of the plant.T001K-BWKEYis the key of the valuation area row.T001K-BUKRSgives the company code of that valuation area.
When valuation is at plant level, which is the usual setup, BWKEY equals WERKS. The join therefore returns one company code per plant.
REPORT zorg_plants_company_codes.
SELECT w~werks,
w~name1,
k~bukrs
FROM t001w AS w
INNER JOIN t001k AS k ON k~bwkey = w~bwkey
INTO TABLE @DATA(lt_plants)
UP TO 20 ROWS.
cl_demo_output=>display( lt_plants ).The query reads plants from T001W and joins each one to its valuation area in T001K. The result lists the plant, its name and the company code it belongs to. A plant can belong to exactly one company code because each plant has one BWKEY, and each BWKEY has one BUKRS.
The reverse direction, "which plants does company code 1000 own?", uses the same join with a condition on k~bukrs.
REPORT zorg_plants_of_company_code.
DATA lv_bukrs TYPE bukrs VALUE '1000'.
SELECT w~werks,
w~name1
FROM t001w AS w
INNER JOIN t001k AS k ON k~bwkey = w~bwkey
WHERE k~bukrs = @lv_bukrs
INTO TABLE @DATA(lt_plants).
cl_demo_output=>display( lt_plants ).Adding the company code name
The company code number alone is hard to read. T001 holds the name in BUTXT. Add a third table to the join.
REPORT zorg_plants_with_names.
SELECT w~werks,
w~name1 AS plant_name,
k~bukrs,
c~butxt AS company_name
FROM t001w AS w
INNER JOIN t001k AS k ON k~bwkey = w~bwkey
INNER JOIN t001 AS c ON c~bukrs = k~bukrs
INTO TABLE @DATA(lt_overview)
UP TO 20 ROWS.
cl_demo_output=>display( lt_overview ).The aliases plant_name and company_name keep the two name columns apart in the result structure. Without them, the inline declaration would fail because the field names would clash with each other.
Storage locations
A storage location row is keyed by plant and storage location number. The query for one plant therefore filters on WERKS alone.
REPORT zorg_storage_locations.
DATA lv_werks TYPE werks_d VALUE '1000'.
SELECT lgort, lgobe
FROM t001l
WHERE werks = @lv_werks
INTO TABLE @DATA(lt_storage_locations).
cl_demo_output=>display( lt_storage_locations ).Two plants can both have a storage location 0001. If you select from T001L by LGORT alone, you will get one row per plant that uses that number. Any join on storage location must therefore use both WERKS and LGORT.
How the tables relate
The diagram shows the chain from company code to stock. Read it from the top: one company code can have many valuation areas, and each valuation area covers its plants. The plants have storage locations and plant-level material data. Stock quantities sit at the bottom, in MARD.
Organisational levels in material tables
Material master data is split across tables by organisational level, and each table's key tells you the level.
| Level | Table | Key (besides `MANDT`) | Typical content |
|---|---|---|---|
| Client | `MARA` | `MATNR` | Material type, base unit of measure, material group |
| Plant | `MARC` | `MATNR`, `WERKS` | MRP settings, procurement type, planning data |
| Valuation area | `MBEW` | `MATNR`, `BWKEY`, `BWTAR` | Valuation class, price control, standard or moving average price |
| Storage location | `MARD` | `MATNR`, `WERKS`, `LGORT` | Stock quantities, such as unrestricted-use stock `LABST` |
MBEW is keyed by valuation area (BWKEY), not by plant, which is why valuation is described as its own level. BWTAR is the valuation type and is blank unless split valuation is used.
The quantity query shows the lowest level:
REPORT zorg_stock_by_location.
DATA lv_werks TYPE werks_d VALUE '1000'.
SELECT matnr, werks, lgort, labst
FROM mard
WHERE werks = @lv_werks
INTO TABLE @DATA(lt_stock).
cl_demo_output=>display( lt_stock ).A material can exist in MARA and still have no MARC row for a plant. In that case it cannot be planned or procured there. A MARD row only exists for storage locations where the material has been extended or has received stock.
Other units and where to find them
Some related units have their own tables, and you will meet them in the same joins.
| Unit | Table | Notes |
|---|---|---|
| Purchasing organisation | `T024E` | Assigned to company codes in `T024E`'s assignments and to plants in `T024W` |
| Sales organisation | `TVKO` | Linked to a company code in the same table (`BUKRS` field) |
| Controlling area | `TKA01` | Company codes are assigned to it in `TKA02` |
Table and field details vary by release. Check them in the ABAP Dictionary (transaction SE11) before you rely on them in code.
Common mistakes
- Assuming a plant row contains its company code. There is no
BUKRScolumn inT001W. Join throughT001K. - Joining storage locations on
LGORTonly. The number is unique only within a plant. Always join onWERKSandLGORT. - Using
T001Was a stock table. It describes the plant. Quantities are inMARDand in other stock tables, such as the special stock tables. - Hardcoding
MANDTin conditions. Let automatic client handling do it. - Treating
BWKEYas always equal toWERKS. That holds for plant-level valuation. If your system values at company code level,BWKEYis the company code.
Exercise
A query joins T001L to MARD using only t001l~lgort = mard~lgort. Why can it return wrong results, and how do you fix it?
Show answer
LGORT is unique only within a plant, so the same storage location number in two plants matches stock from both. The result mixes plants. Fix it by also joining on the plant: t001l~werks = mard~werks AND t001l~lgort = mard~lgort.
Exercise
You need the company code name for each plant. Which tables do you read, and which fields connect them?
Show answer
Read T001W, T001K and T001. Join T001W to T001K on BWKEY (valuation area). Join T001K to T001 on BUKRS. The name is in T001-BUTXT.