In brief
Moving from DDS to SQL in Db2 for i means describing database files with CREATE TABLE, CREATE INDEX, and CREATE VIEW statements instead of DDS specifications. It gives you primary and foreign keys, constraints, long names, data checks on write, and access to new database features. You can do it file by file, without rewriting RPG programs, if you keep the record format name and the field layout. Start by generating DDL from existing files and by testing on a copy of the data.
DDS and SQL: the same database, two ways to describe it
Db2 for i is the database built into IBM i. A physical file described in DDS and a table created with CREATE TABLE are objects of the same database: RPG reads both, and SQL queries both. They differ in how they are defined and in a few behaviors.
| Feature | DDS (physical and logical files) | SQL (tables, indexes, views) |
|---|---|---|
| Description | Column-based specifications, compiled with CRTPF/CRTLF | DDL statements, a script in the repository |
| Data checks | On read | On write, bad data does not reach the table |
| Keys and constraints | A key in the file, constraints only through a separate ADDPFCST command | PRIMARY KEY, FOREIGN KEY, CHECK, UNIQUE in the table definition |
| Names | Up to 10 characters | Long names with a short system name |
| New types and functions | Limited | Available right away, for example identity columns, timestamps with a default value |
The most important practical difference is the moment of data checking. You cannot write an invalid decimal value to an SQL table, while a DDS file may accept it and the error appears only on read, in another program and on another day.
What moving to SQL gives you
The benefits show up in three places.
- Data quality. Foreign keys and CHECK constraints move some rules from programs into the database. An order with no customer or a negative quantity will not be saved, whichever program tries.
- Readability for other systems. Long column names (nr_klienta instead of NRKL) and descriptions in the database catalog mean that the data warehouse, reports, and REST services use the database without a dictionary of abbreviations.
- Versioning. The DDL script sits in git, goes through review, and can be run on a test environment. Changing the database stops being a manual operation on the server.
Add to this the evolution of the database: new Db2 for i features appear mainly in SQL, while the DDS description has changed little for years. A team that knows SQL from other databases finds its way around tables faster than around column-based specifications.
What a table described in SQL looks like
Below is an example of a table with long names, short system names for RPG programs, and a record format name.
CREATE TABLE app.klienci_hurtowi FOR SYSTEM NAME klihurt (
nr_klienta FOR COLUMN nrkl DECIMAL(7, 0) NOT NULL,
nazwa_klienta FOR COLUMN nazwa CHAR(40) NOT NULL DEFAULT '',
nip FOR COLUMN nip CHAR(10) NOT NULL DEFAULT '',
utworzono FOR COLUMN utworz TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (nr_klienta)
) RCDFMT klihurtr;
LABEL ON TABLE app.klienci_hurtowi IS 'Klienci hurtowi';The FOR SYSTEM NAME clause gives the object a short name, FOR COLUMN a short column name, and RCDFMT the record format name that RPG uses for record operations. A program written long ago for a DDS file can therefore run on an SQL table with no code changes.
It is also worth knowing where the table is created. A schema created with CREATE SCHEMA has its own journal (QSQJRN), and tables created in it are journaled right away. The journal gives change history and transaction control (COMMIT and ROLLBACK), which many old libraries with DDS files did not have. When moving files to a library without a journal, you have to start journaling yourself, otherwise programs with transaction control will report an error.
A file-by-file migration plan
It is safest to make the move gradually, starting with the files that most often cause trouble or are needed by new integrations.
- Inventory. A list of physical and logical files, the programs that use them (the DSPDBR and DSPPGMREF commands), and their size and frequency of change.
- Generating DDL. The QSYS2.GENERATE_SQL procedure creates SQL statements from existing objects. The result is a starting point: you need to review it and add keys, constraints, and long names.
- Format compatibility. Keep the record format name and the names, types, and order of the fields. Compare the format level identifier (DSPFD) before and after the change. If it differs, programs that use the file have to be recompiled.
- Logical files. Replace them with SQL indexes for keys and views for selecting fields and records. Programs that read a logical file by key need an index with the same format name, or a logical file left in place over the new table.
- Data copy and tests. Move the data on a test environment, run the batch processes, and compare the results with the old version. Bad data that DDS accepted will show up in this step as write errors.
- Deployment in a maintenance window. Changing a file requires exclusive access, so you need a window with no user work and no batch jobs, with a rollback plan.
Step 5 often reveals the most: records with invalid decimal values, dates stored as numbers with zeros, text fields containing binary data. It is better to find this in a test than in the nightly run. An approach to comparing data after a migration is shown in the scenario data migration tests when merging two banks.
SQL in programs and system services
Moving to DDL is a good moment to read data in new RPG code with SQL queries instead of record operations. A single query with a JOIN often replaces several loops with CHAIN and READE, and the optimizer chooses the index itself.
SQL services in the QSYS2 schema also give insight into the system without green screen commands, for example into active jobs or object statistics:
SELECT job_name, authorization_name, cpu_time
FROM TABLE(QSYS2.ACTIVE_JOB_INFO()) AS j
ORDER BY cpu_time DESC
FETCH FIRST 10 ROWS ONLY;This is useful for diagnosing performance and for documenting which programs and files are actually used. We describe finding bottlenecks in the database in general in the article Database bottlenecks: how to find them before production, and the place of this change in a broader plan in the article IBM i modernization: refactoring, APIs, or migration.
Questions and answers
Do you have to rewrite RPG programs after moving from DDS to SQL?
Usually not. If the new table has the same record format name, field names, types, and order, RPG programs use it just like a DDS file. It is worth checking the format level identifier before and after the change. If it differs, recompiling the programs is enough, with no code changes, unless the field layout has changed.
What replaces DDS logical files?
A logical file with a key is replaced by an SQL index, and a logical file with field or record selection by an SQL view. A view has no key, so RPG programs that read it by key need an index. For logical files that join several files, each program has to be checked separately, because the behavior on read may differ.
Will data from DDS files move to an SQL table without problems?
Not always. An SQL table checks data on write, so records with invalid decimal values or wrong dates that a DDS file accepted will raise an error on copy. That is why data is copied on a test environment before deployment, the errors are collected, and how to fix them is agreed with the business. This step often reveals data errors from years ago.
Where to start if there are hundreds of files?
With the files needed by new integrations and reports, or those that most often cause data quality problems. Create new tables in SQL right away. For the rest, prepare an inventory and a plan, and do the move alongside changes to the programs that use them. Generating DDL with the QSYS2.GENERATE_SQL procedure speeds up the work on every file.
Sources