Generating SQL to re-create a Db2 object (reverse engineering)
Before making changes to a Db2 object, you might find it useful to generate the SQL statements that are required to re-create that object. Generating this SQL ensures that the changes are applied to the current definition and that the original object definitions are available for fallback purposes.
About this task
This process of generating the SQL to re-create an object is called reverse engineering. When requested, the reverse engineering function (also called the GEN function) of Db2 Admin Tool extracts the SQL for an object from the Db2 catalog.
In addition to extracting the DDL (data definition language) for objects, you can also generate the DCL (data control language) for all authorizations on the objects and the DML (data manipulation language) for the catalog statistics for the objects.
When you request DDL for databases, table spaces, and tables, you can request that DDL also be generated for all dependent objects, including table spaces, tables, indexes, views, synonyms, aliases, referential constraints, table check constraints, and triggers. When you request DDL for schemas, you can request that DDL also be generated for the associated distinct types, sequences, functions, global variables, stored procedures, tables, indexes, aliases, and views. Alternatively, you can specify objects that you want excluded from the generated DDL.
You can generate the SQL statements online or with a batch job. Batch jobs are recommended when you extract many objects from a large catalog.
The DDL generator function is available as a REST API. See Provided REST APIs.
- Db2 Admin Tool does not extract IDCAMS DEFINE CLUSTER statements for VCAT-defined table spaces and indexes.
- When you reconstruct an external SQL procedure, Db2 Admin Tool cannot recover the original procedure body unless you used DSNTPSMP to create the procedure. If you used DSNTPSMP, Db2 Admin Tool can retrieve the stored procedure body from the catalog. Otherwise, if you did not use DSNTPSMP, Db2 Admin Tool cannot recreate the procedure body and replaces the original procedure body with the string "LEAVE L0". In this case, the procedure body cannot be recovered because it is not stored in the catalog. For more information about DSNTPSMP, see Creating an external SQL procedure by using DSNTPSMP (Db2 12 for z/OS).
- When the size of a native SQL procedure statement is near the 2 MB limit, sometimes
Db2 Admin Tool cannot generate the DDL for
the native SQL procedure statement. This situation can occur for one of the following reasons:
- Extra options are generated: The native SQL procedure statement is created by GEN by first constructing the native SQL procedure options from the Db2 catalog and then appending the native SQL procedure body that is stored in the TEXT column of SYSIBM.SYSROUTINES. Sometimes the resulting DDL statement exceeds 2 MB. This situation can occur, because Db2 Admin Tool generated more options than were specified when the native SQL procedure was created. (More options can be generated when the DB2 defaults handling option is set to Keep.) When the 2 MB limit is exceeded, Db2 Admin Tool issues the warning message ADB1915W and generates the native SQL procedure DDL as it is stored in Db2. The resulting DDL for the native SQL procedure object is the exact contents of the TEXT column in SYSIBM.SYSROUTINES. If masking or an override was specified (such as a change owner, change schema, or Run SQLID), the ADB1916E error message is issued instead and processing stops. Processing stops, because Db2 Admin Tool cannot satisfy the 2 MB limit for the native SQL procedure DDL with the specified masks or overrides.
- Bytes were added for formatting: Db2 Admin Tool attempts to format each DDL statement for readability. Sometimes during the formatting process, the extra bytes that were added for formatting cause the formatted statement length to exceed 2 MB. When this situation occurs, Db2 Admin Tool issues the ADB1919W warning message and generates unformatted DDL for the native SQL procedure. If masking or an override was specified (such as change owner, change schema, or Run SQLID), the ADB1920E error message is issued instead and processing stops. Processing stops, because Db2 Admin Tool cannot satisfy the 2 MB limit for the native SQL procedure DDL with the specified masks or overrides.
Procedure
To generate SQL to re-create a Db2 object: