Sub topic 8
Loading data through staging tables
This chapter assumes you know the three transfer approaches from the earlier chapter and have a migration project with objects selected. It covers the staging tables approach: what it is, how the data flows, and where it fails.
What the staging tables approach is
With the staging tables approach, you do not upload a file for each migration object. You write your legacy data into database tables, and the migration cockpit reads from those tables. The cockpit generates the tables when you create the project. Each source structure of a migration object gets its own table, so an object with a header and several sub-structures gets several tables.
The generated tables live in an SAP HANA database. This is either the database of the S/4HANA system itself or a separate SAP HANA database that you connect. The cockpit does not care how the rows get into the tables. You can use SQL inserts, an ETL tool such as SAP Data Services, or any other tool that can write to SAP HANA.
The approach suits these situations:
- The data volume is large, and file uploads become slow or hit size limits.
- The data is already in a database or comes from several sources that need joins or cleansing.
- You want to repeat loads often during test cycles without re-creating files.
- A team or tool already exists that handles extraction and transformation.
It is a poor fit for a small one-off load of a few hundred rows. A file is faster to set up in that case.
How the data flows
The cockpit still performs the steps you know: validation, value mapping, simulation, and migration. Only the way data enters changes.
The staging tables are the boundary between your work and the cockpit's work. Your side ends when the rows are in the tables. Everything after that is the same predefined processing that every migration object applies.
Setting up the project
You choose the approach when you create the migration project. In the project creation dialog, select the option to transfer data using staging tables. Then select the migration objects as usual. The choice applies to the whole project, so a project is either file-based or staging-based. To mix approaches, create two projects.
You also decide which database holds the tables. Two options exist:
| Option | Where the tables live | When to use it |
|---|---|---|
| Local schema | The SAP HANA database of the S/4HANA system | Smaller loads, simple landscapes |
| Remote database connection | A separate SAP HANA database that you connect to the system | Large volumes, or when you want to keep staging load away from the application database |
Check the current SAP documentation for your release. The set of supported connection types and the exact dialog labels differ between on-premise and cloud editions and change between releases.
Finding and filling the tables
After the project exists, open a migration object in the cockpit. It lists its staging tables and the fields of each one. The table names are generated, so do not guess them. Copy them from the cockpit. The same applies to field names and their order.
Some rules apply when you fill the tables:
- Match the field definitions. Staging fields are typically text-like. Dates, numbers, and flags must follow the format the object expects, such as the date format and decimal notation shown in the object's field list. Wrong formats are the most common cause of validation messages.
- Keep the key fields consistent across tables. The sub-structures of one object point to their header row through key fields. A child row with a key that has no header row cannot be assigned to a record.
- Load mandatory fields. The cockpit marks mandatory fields. An empty mandatory field produces an error for that record.
- Do not change the table structure. If you need extra fields or fewer fields, use the migration object modeler instead. A table that you altered by hand no longer matches what the cockpit expects.
- Load in chunks if the volume is large. Commit in batches so that a failed insert does not roll back an entire multi-hour load.
A pre-check before the load catches the cheapest errors early. The script below reads a legacy CSV extract and reports duplicate keys and values that are too long. Run it on the extract before you write it into the staging tables. The file and field names are placeholders, not SAP template fields. Adjust them to your source and to the field lengths in your object.
import csv
from collections import Counter
def precheck(path, key_field, max_lengths):
"""Return a list of problems found in a source CSV before upload."""
problems = []
keys = Counter()
with open(path, newline="", encoding="utf-8") as f:
for line_no, row in enumerate(csv.DictReader(f), start=2):
keys[row[key_field]] += 1
for field, limit in max_lengths.items():
if len(row[field]) > limit:
problems.append(f"line {line_no}: {field} is longer than {limit} characters")
problems += [f"duplicate key {k}" for k, n in keys.items() if n > 1]
return problems
for problem in precheck("customers.csv", "CustomerId", {"Name": 40}):
print(problem)The script prints one line per problem and prints nothing if the file is clean. The line numbers start at 2 because line 1 is the header. This check does not replace the cockpit's validation. It only keeps obvious defects out of the tables.
Starting the transfer in the cockpit
When the tables contain data, go back to the cockpit and start the transfer for the object. The cockpit then reads the rows from the staging tables into its own processing area. You can then validate the data, work on value mappings, and run a simulation.
Two details matter in practice:
- The cockpit works on a copy. After the transfer, changes you make to the staging tables do not affect data that the cockpit has already taken over. To apply a correction, fix it in the staging table and transfer again, or follow the correction options that the cockpit offers for that object in your release.
- Reloading replaces earlier content. A new transfer for an object generally supersedes the previously transferred data for that object. Do not assume the rows are appended. Confirm the behavior in your release on a test object before you rely on it.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Records are missing after the transfer | Rows were not committed, or were written to the wrong schema | Check the row count in the tables, and check the schema against the project settings |
| Child records are rejected or ignored | The key does not match any header row | Compare key values across the tables of the object |
| Many format messages in validation | Dates or numbers are not in the expected format | Convert them in the ETL step, not in the cockpit |
| The load job fails on insert | Value too long for the staging field | Use the pre-check to find long values and shorten or split them |
| The tables look different after a release upgrade | The generated structure changed | Regenerate the tables and adapt the load job |
Many of these appear first as validation messages. The chapter on validation and the chapter on messages and error correction explain how to read them. The rule that follows from this table is to correct errors at the source or in the ETL step, because staging tables are meant to be reloaded.
Trade-offs
The staging approach moves effort from the cockpit to your own load process. You gain speed, repeatability, and the freedom to use any tool. You also own more things: the database access, the table filling, the monitoring of the load job, and the cleanup of old test data between cycles. Users need database authorizations in addition to the cockpit authorizations, and a clear owner for those rights.
Exercise
Your team must migrate 2 million material records from a legacy database that also needs joins and cleansing, and you plan to repeat the load in four test cycles. Which approach fits better, a file upload or staging tables? Name two reasons, and say who is responsible for the table structure.
Show answer
Staging tables fit better. The volume is large, which makes file uploads slow and awkward. The data needs joins and cleansing, which an ETL tool can do on its way into the tables, and the same job can be re-run in each test cycle. The cockpit generates the table structure, so you must not alter it. Your load job must adapt to the generated tables and field definitions.
Exercise
A child record in a staging table is not picked up after the transfer, although its own fields are valid. What is the most likely cause, and where do you look first?
Show answer
Its key does not match any header row in the object's header staging table. Compare the key values between the header table and the child table first.