DB2 Version 9.7 for Linux, UNIX, and Windows

Building embedded SQL applications

Once you have created the source code for your embedded SQL application, you must follow additional steps to build it. You should consider building 64-bit executables when developing new embedded SQL database applications. Along with compiling and linking your program, you must precompile and bind it.

The precompilation process converts embedded SQL statements into DB2® run-time API calls that a host language compiler can process. By default, a package is created at precompile time. Optionally, a bind file can be created at precompile time. The bind file contains information about the SQL statements in the application program. The bind file can be used later with the BIND command to create a package for the application.

Binding is the process of creating a package from a bind file and storing it in a database. The bind file must be bound to each database that needs to be accessed by the application. If your application accesses more than one database, you must create a package for each database.

To run applications written in compiled host languages, you must create the packages needed by the database manager at execution time. The following figure shows the order of these steps, along with the various modules of a typical compiled DB2 application.:

  1. Create source files that contain programs with embedded SQL statements.
  2. Connect to a database, then precompile each source file to convert embedded SQL source statements into a form the database manager can use.

    Since the SQL statements placed in an application are not specific to the host language, the database manager provides a way to convert the SQL syntax for processing by the host language. For C, C++, COBOL, or FORTRAN languages, this conversion is handled by the DB2 precompiler that is invoked using the PRECOMPILE (or PREP) command. The precompiler converts embedded SQL statements directly into DB2 run-time services API calls. When the precompiler processes a source file, it specifically looks for SQL statements and avoids the non-SQL host language.

  3. Compile the modified source files (and other files without SQL statements) using the host language compiler.
  4. Link the object files with the DB2 and host language libraries to produce an executable program.

    Compiling and linking (steps 3 and 4) create the required object modules

  5. Bind the bind file to create the package if this was not already done at precompile time, or if a different database is going to be accessed. Binding creates the package to be used by the database manager when the program is run.
  6. Run the application. The application accesses the database using the access plans.
Figure 1. Preparing Programs Written in Compiled Host Languages
Preparing programs written in compiled host languages