IBM i

IBM i 

A space for professionals working with IBM’s integrated OS for Power systems to exchange ideas, ask questions, and share expertise on topics like RPG and COBOL development, application modernization, open source integration, system administration, and business continuity.


#Power


#IBMi
 View Only

Unlocking the Blueprint: A Guide to Retrieving DDL Sources in IBM i

By Nirmal Khatri posted 17 days ago

  

Are you a developer or system administrator struggling with database migrations, replications, or simply understanding an object's structure?  

You are not alone.  

 

In modern IBM i environments, where SQL is the preferred method for creating database objects, the original source code is often not kept in production. This can make it difficult to see an object's complete definition, especially when you need it for troubleshooting or impact analysis. 

 

Fortunately, IBM i offers a powerful solution: retrieving the Data Definition Language (DDL). 

 

Method 1: The GENERATE_DDL Function 

For quick, on-the-fly DDL retrieval, the QSYS2.GENERATE_DDL function is your go-to tool. This function returns a single row with the full DDL statement for a specified object. 

 

To use it, simply provide the library name (schema) and the object name. You can also specify the object type for more precise results. 

 

Example: 

To get the DDL for a table called ORDERS in the SALESDATA library, you can run this query: 

 

SELECT DDL  

FROM TABLE(QSYS2.GENERATE_DDL(OBJECT_SCHEMA => 'SALESDATA',  

                        OBJECT_NAME => 'ORDERS', 

                        OBJECT_TYPE => 'TABLE')); 
 

This is an efficient way to get the exact CREATE statement needed for tasks like documentation or quick object recreation. 

 

Method 2: The GENERATE_SQL_OBJECTS Procedure 

For a more automated approach, especially when you need to generate DDL for multiple objects or save the results to a file, use the QSYS2.GENERATE_SQL_OBJECTS procedure. 

 

Step 1: Create an Input Table 

First, create a temporary table to list the objects you want to process. This table must have three specific columns: OBJECT_SCHEMA, OBJECT_NAME, and SQL_OBJECT_TYPE. 

 

CREATE TABLE QTEMP.INPUTTBL( 
    OBJECT_SCHEMA    VARCHAR(258), 
    OBJECT_NAME         VARCHAR(258), 
    SQL_OBJECT_TYPE CHAR(10) 
); 
 

Step 2: Insert Object Details 

Next, insert the details of the SQL objects into your new table. For example, to generate the DDL for a table named TRS in the QTEMP library, you would use this command: 

 

INSERT INTO QTEMP.INPUTTBL VALUES ('QTEMP', 'TRS', 'TABLE'); 
 

You can add as many objects as you need to this table. 

 

Step 3: Run the Procedure 

Finally, call the GENERATE_SQL_OBJECTS procedure, telling it where to find your input list and where to save the output. The DDL will be saved as a source member in a specified library and source file. 

 

CALL QSYS2.GENERATE_SQL_OBJECTS( 
    SYSTEM_TABLE_NAME => 'INPUTTBL', 
    SYSTEM_TABLE_SCHEMA => 'QTEMP', 
    DATABASE_SOURCE_FILE_NAME => 'QDDSSRC', 
    DATABASE_SOURCE_FILE_LIBRARY_NAME => 'PIOLIB', 
    DATABASE_SOURCE_FILE_MEMBER => 'TDDLSRC' 
); 
 

This command will create a new member named TDDLSRC in the QDDSSRC source physical file within the PIOLIB library, containing the DDL for all the objects you listed. 

 

Additional Procedure Parameters 

These are the parameters you can use to customize the generated SQL: 

  • STATEMENT_FORMATTING_OPTION: This parameter controls the formatting of the SQL statements. 

  • '0': No extra formatting is applied. 

  • '1': Adds new lines and tabs to make the code easier to read. 

 

  • COMMENT_OPTION: This parameter decides if comments are included in the SQL. The default value is '1'. 

  • '0': No comments are generated. 

  • '1': Comments are included for TABLE or VIEW objects. 

 

  • LABEL_OPTION: This parameter determines whether labels are generated. The default value is '1'. 

  • '0': Labels are not generated. 

  • '1': Labels are generated for TABLE or VIEW objects. 

 

  • CCSID_OPTION: This parameter controls if CCSID attributes are included. The default value is '1'. 

  • '0': CCSID attributes are not generated. 

  • '1': CCSID attributes are generated. 

 

 

 

Why DDL Retrieval is Essential 

  • Simplifies Documentation: Quickly get a text-based record of your database structure. 

  • Facilitates Migration: Ensure perfect recreation of objects when moving them between different environments. 

  • Aids Troubleshooting: Examine the DDL to quickly identify missing constraints or structural issues. 

  • Enables Version Control: Store your DDL in a system like Git to track changes over time. 

 

While these tools are incredibly useful, remember that they currently support a limited variety of objects, including aliases, constraints, functions, and indexes. 

 

By leveraging these powerful SQL tools, you can easily maintain, replicate, and document your database schema on IBM i. 

 

Conclusion 

In IBM i, we can seamlessly utilize SQL to regenerate the DDL source of the mentioned object types. Whether you prefer using SQL commands, web-based tools like IBM Navigator for i, or desktop applications like IBM i Access Client Solutions, IBM i offers robust solutions for generating and managing DDL. It is easy to replicate, maintain, and document your database schema by leveraging these tools. 

1 comment
11 views

Permalink

Comments

16 days ago

It is strange that I cannot find documentation about a "QSYS2.GENERATE_DDL()" function anywhere, including in the official Db2 for i documentation:

GENERATE_SQL—sure. GENERATE_DDL—did an AI dream up that function?

Kurt