Processing SQL statements by using the Db2 precompiler
The Db2 precompiler scans a program and copies all of the SQL statements and host variable information into a database request module (DBRM ). The Db2 precompiler also returns source code that has been modified so that the SQL statements do not cause errors when you compile the program.
Before you begin
Ensure that your application development programming languages meet the minimum requirements listed in "Building applications by using the Db2 precompiler" in Db2 12 Program Directory. See Program directories for Db2 12.
The Db2 coprocessor is the recommended method for processing SQL statements in application programs. Compared to the Db2 precompiler, the Db2 coprocessor has fewer restrictions on SQL programs, and more fully supports the latest SQL and programming language enhancements. See Processing SQL statements by using the Db2 coprocessor.
If you are using the Db2 precompiler, ensure that the names of host variables and host-variable arrays are unique within the program, even if the variables and variable arrays are in different blocks, classes, procedures, functions, or subroutines. You can qualify the names with a structure name to make them unique.
About this task
After the SQL statements and host variable information are copied into a DBRM and the modified source code is returned, you can compile and link-edit this modified source code.
The following figure illustrates the program preparation process when you use the Db2 precompiler. After you process SQL statements in your source program by using the Db2 precompiler, you create a load module, possibly one or more packages, and an application plan. Creating a load module involves compiling the modified source code that is produced by the precompiler into an object program, and link-editing the object program to create a load module. Creating a package or an application plan, a process unique to Db2, involves binding one or more DBRMs, which are created by the Db2 precompiler, using the BIND PACKAGE command.
Before you run the Db2 precompiler, use DCLGEN to obtain accurate SQL DECLARE TABLE statements. Db2 precompiler checks table and column references against SQL DECLARE TABLE statements in the program, not the actual tables and columns.
Db2 does not need to be active when you precompile your program.
You do not need to precompile the program on the same Db2 subsystem on which you bind the DBRM and run the program. You can bind a DBRM and run it on a Db2 subsystem at the previous release level, if the original program does not use any properties of Db2 that are unique to the current release. You can also run applications on the current release that were previously bound on subsystems at the previous release level.
Procedure
To process SQL statements by using the Db2 precompiler:
Results
The main output from the Db2 precompiler is a database request module (DBRM). However, the Db2 precompiler also produces modified source statements, a list of source statements, a list of statements that refer to host names and columns, and diagnostics. For more information, see Output from the Db2 precompiler.
What to do next
- Preparing a program with object-oriented extensions by using JCL
-
If your C++ or Enterprise COBOL for z/OS® program satisfies both of these conditions, you need special JCL to prepare it:
- The program consists of more than one data set or member.
- More than one data set or member contains SQL statements.
You must precompile the contents of each data set or member separately, but the prelinker must receive all of the compiler output together.
JCL procedure DSNHCPP2, which is in member DSNTIJMV of data set DSN1210.SDSNSAMP, shows you one way to do this for C++.
- Precompiling a batch program
- When you add SQL statements to an application program, you must precompile the application program and bind the resulting DBRM into a package, as described in Preparing an application to run on Db2 for z/OS.