Building C/C++ routines on Windows
sqllib\samples\c
and
sqllib\samples\cpp
directories. The directories also contain sample programs that
you can build with these files.
You can find the commands to build
embedded SQL routines, such as stored procedures and user-defined functions, in the
bldrtn.bat
batch file.About this task
The batch file builds a DLL on the server. It takes two
parameters, represented inside the batch file by the variables %1
and %2
.
The
first parameter, %1
, specifies the name of your source
file. The batch file uses the source file name for the DLL name. The
second parameter, %2
, specifies the name of the database
to which you want to connect. Since the DLL must be built on the same
instance where the database resides, there are no parameters for user
ID and password.
Only the first parameter, the source file name,
is required. Database name is optional. If no database name is supplied,
the program uses the default sample
database.
The following examples show you how to build routine DLLs with:
- stored procedures
- non-embedded SQL user-defined functions (UDFs)
- embedded SQL user-defined functions (UDFs)
- Stored procedure DLL
- To build the
spserver
DLL from either the C source file,spserver.sqc
, or the C++ source file,spserver.sqx
:- Enter the batch file name and program name:
If connecting to another database, also enter the database name:bldrtn spserver
The batch file uses the module definition filebldrtn spserver database
spserver.def
, contained in the same directory as the sample programs, to build the DLL. The batch file copies the DLL,spserver.dll
, to the server in the pathsqllib\function
. - Next, catalog the routines by running the
spcat
script on the server:
This script connects to the sample database, uncatalogs the routines if they were previously cataloged by callingspcat
spdrop.db2
, then catalogs them by callingspcreate.db2
, and finally disconnects from the database. You can also call thespdrop.db2
andspcreate.db2
scripts individually. - Then, stop and restart the database to allow the new DLL to be recognized. If necessary, set the file mode for the DLL, so that the Db2 instance can access it.
- Enter the batch file name and program name:
Once you build the DLL, spserver
,
you can build the client application spclient
that
calls it.
You can build spclient
by using
the batch file, bldapp.bat
.
To call the DLL, 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.
The client application accesses the DLL, spserver
,
and executes a number of routines on the server database. The output
is returned to the client application.
- Non-embedded SQL UDF DLL
- To build the user-defined function
udfsrv
from the source fileudfsrv.c
, enter:bldrtn udfsrv
The batch file uses the module definition file,
udfsrv.def
, contained in the same directory as the sample program files, to build the user-defined function DLL. The batch file copies the user-defined function DLL,udfsrv.dll
, to the server in the pathsqllib\function
.Once you build
udfsrv
, you can build the client application,udfcli
, that calls it. CLI, as well as embedded SQL C and C++ versions of this program are provided.You can build the CLI
udfcli
program from theudfcli.c
source file insqllib\samples\cli
using the batch filebldapp
.You can build the embedded SQL C
udfcli
program from theudfcli.sqc
source file insqllib\samples\c
using the batch filebldapp
.You can build the embedded SQL C++
udfcli
program from theudfcli.sqx
source file insqllib\samples\cpp
using the batch filebldapp
.To run the UDF, enter:udfcli
The calling application calls the
ScalarUDF
function from theudfsrv
DLL.
- Embedded SQL UDF DLL
- To build the embedded SQL user-defined function library
udfemsrv
from the C source fileudfemsrv.sqc
insqllib\samples\c
, or from the C++ source fileudfemsrv.sqx
insqllib\samples\cpp
, enter:
If connecting to another database, also enter the database name:bldrtn udfemsrv
bldrtn udfemsrv database
The batch file uses the module definition file,
udfemsrv.def
, contained in the same directory as the sample programs, to build the user-defined function DLL. The batch file copies the user-defined function DLL,udfemsrv.dll
, to the server in the pathsqllib\function
.Once you build
udfemsrv
, you can build the client application,udfemcli
, that calls it. You can buildudfemcli
from the C source fileudfemcli.sqc
insqllib\samples\c
, or from the C++ source fileudfemcli.sqx
insqllib\samples\cpp
using the batch filebldapp
.To run the UDF, enter:udfemcli
The calling application calls the UDFs in the
udfemsrv
DLL.