Building routines in C or C++ using the sample build script (UNIX)
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
spserverfrom the source filespserver.sqcfor C andspserver.sqCfor C++:- If connecting to the
sampledatabase, enter the build script name and program name:
If connecting to another database, also enter the database name:bldrtn spserver
The script copies the shared library to the server in the pathbldrtn spserver databasesqllib/function. - Next, catalog the routines by running the
spcatscript on the server:
This script connects to the sample database, uncatalogs the routines if they were previously cataloged by callingspcatspdrop.db2, then catalogs them by callingspcreate.db2, and finally disconnects from the database. You can also call thespdrop.db2andspcreate.db2scripts individually. - 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
db2stopfollowed bydb2starton the command line.
- If connecting to the
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
- 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.
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 fileudfemsrv.sqcfor C andudfemsrv.sqCfor C++, if connecting to thesampledatabase, enter the build script name and program name:
If connecting to another database, also enter the database name:bldrtn udfemsrv
The script copies the UDF to thebldrtn udfemsrv databasesqllib/functiondirectory.
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
- 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.
LD_LIBRARY_PATH operating
system environment variable is ignored when embedded SQL procedures
are built.