Building routines in C or C++ using the sample build script (UNIX)

You are provided with build scripts for compiling and linking C and C++ programs. These scripts are located in the sqllib/samples/c directory for C routines and the sqllib/samples/cpp directory for C++ routines. The directories also contain sample programs that you can build with these scripts.

About this task

The script, bldrtn, contains the commands to build routines (stored procedures and user-defined functions). The script compiles the routines into a shared library that can be loaded by the database manager and called by a client application.

The first parameter, $1, specifies the name of your source file. The second parameter, $2, specifies the name of the database to which you want to connect.

The database parameter is optional. If no database name is supplied, the program uses the default sample database. There are no parameters for user ID and password, because the stored procedure must be built on the same instance where the database resides.

The following examples show you how to build routine shared libraries with:

  • stored procedures
  • non-embedded SQL user-defined functions (UDFs)
  • embedded SQL user-defined functions (UDFs)
Stored procedure shared library

To build the sample program spserver from the source file spserver.sqc for C and spserver.sqC for C++:

  1. If connecting to the sample database, enter the build script name and program name:
       bldrtn spserver
    If connecting to another database, also enter the database name:
       bldrtn spserver database
    The script copies the shared library to the server in the path sqllib/function.
  2. Next, catalog the routines by running the spcat script on the server:
       spcat
    This script connects to the sample database, uncatalogs the routines if they were previously cataloged by calling spdrop.db2, then catalogs them by calling spcreate.db2, and finally disconnects from the database. You can also call the spdrop.db2 and spcreate.db2 scripts individually.
  3. Then, if this is not the first time the stored procedure is built, stop and restart the database to ensure the new version of the shared library is recognized. You can do this by entering db2stop followed by db2start on the command line.

Once you build the shared library, spserver, you can build the client application, spclient, that accesses the shared library.

You can build spclient by using the script, bldapp.

To call the stored procedures in the shared library, run the sample client application by entering: spclient database userid password

where
database
Is the name of the database to which you want to connect. The name could be sample, or its alias, or another database name.
userid
Is a valid user ID.
password
Is a valid password for the user ID.
The client application accesses the shared library, spserver, and executes a number of stored procedure functions on the server database. The output is returned to the client application.
Embedded SQL UDF shared library
To build the embedded SQL user-defined function program, udfemsrv, from the source file udfemsrv.sqc for C and udfemsrv.sqC for C++, if connecting to the sample database, enter the build script name and program name:
   bldrtn udfemsrv
If connecting to another database, also enter the database name:
   bldrtn udfemsrv database
The script copies the UDF to the sqllib/function directory.

Once you build udfemsrv, you can build the client application, udfemcli, that calls it. You can build the udfemcli client program from the source file udfemcli.sqc, in sqllib/samples/c, using the script, bldapp.

To call the UDFs in the shared library, run the client application by entering: udfemcli database userid password

where
database
Is the name of the database to which you want to connect. The name could be sample, or its alias, or another database name.
userid
Is a valid user ID.
password
Is a valid password for the user ID.

The client application accesses the shared library, udfemsrv, and executes the user-defined functions on the server database. The output is returned to the client application.

Note: The LD_LIBRARY_PATH operating system environment variable is ignored when embedded SQL procedures are built.