Sub topic 11
Where organisational units live in the database
This chapter assumes you know what a client, company code, plant, and storage location are, and that you can read basic Open SQL (SAP's database query dialect in ABAP). You also need to know what a primary key is. Here you will learn which tables hold these units and how their keys show the hierarchy.
The core tables
Each organisational unit has a main definition table. Most of these are customizing tables. Customizing is the configuration data an administrator maintains, as opposed to business data like orders.
| Unit | Table | Key fields | Notes |
|---|---|---|---|
| Client | T000 | MANDT | One row per client in the system |
| Company code | T001 | MANDT, BUKRS | Holds currency and chart of accounts |
| Plant | T001W | MANDT, WERKS | Holds plant name and address data |
| Valuation area | T001K | MANDT, BWKEY | Links the plant to a company code |
| Storage location | T001L | MANDT, WERKS, LGORT | Unique only within a plant |
MANDT is the client field. Almost every table starts with it as the first key field. When you write Open SQL, the system adds the client filter for you, so you rarely type it.
The keys show the hierarchy
Compare the key fields in the table above. A storage location key is WERKS plus LGORT. The same LGORT value, such as 0001, can exist in many plants. This is why a storage location is unique only within its plant.
A plant key is just WERKS. The company code is not part of it. The plant's company code comes from a separate assignment, stored in T001K.
Joining plants to company codes
A plant has no company code field of its own. You reach it through the valuation area. When valuation is at plant level, BWKEY equals WERKS.
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.The join runs on BWKEY. T001W also carries a BWKEY field, so the plant points to its valuation area. T001K then gives the company code in BUKRS. Because each valuation area has one company code, each plant gets exactly one.
Listing storage locations
To list the storage locations of one plant, filter T001L by WERKS.
DATA lv_werks TYPE werks_d VALUE '1000'.
SELECT lgort, lgobe
FROM t001l
WHERE werks = @lv_werks
INTO TABLE @DATA(lt_storage_locations).If you select by LGORT alone, you get rows from several plants. Always include WERKS in your condition.
Where stock and material data are held
The organisational tables only define the units. Material data is stored in other tables that use these units as key fields. MARD holds stock per material, plant, and storage location. Its key is MATNR, WERKS, and LGORT.
DATA lv_werks TYPE werks_d VALUE '1000'.
SELECT matnr, werks, lgort, labst
FROM mard
WHERE werks = @lv_werks
INTO TABLE @DATA(lt_stock).LABST is unrestricted-use stock. This is stock that can be used freely. Quantities exist at storage location level, so MARD is the table to read for stock.
Common mistakes
- Reading
T001Lwithout aWERKScondition and treatingLGORTas globally unique. - Looking for a company code field in
T001W. UseT001K. - Summing
MARDrows without grouping by plant, which mixes stock from different sites. - Using customizing tables as if they held transaction data. They hold definitions only.
Exercise
You need the company code for every storage location in plant 1000. Which tables do you use, and what keys link them?
Show answer
Use T001L, T001W, and T001K. T001L links to T001W through WERKS. T001W links to T001K through BWKEY. The company code is BUKRS in T001K. Filter T001L on WERKS = '1000'.
Exercise
Why does the key of T001L include WERKS?
Show answer
A storage location is unique only within its plant. The same LGORT value can exist in several plants, so WERKS is needed to identify one row.