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
spserverDLL 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 databasespserver.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
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, 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
udfsrvfrom the source fileudfsrv.c, enter:bldrtn udfsrvThe 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
udfcliprogram from theudfcli.csource file insqllib\samples\cliusing the batch filebldapp.You can build the embedded SQL C
udfcliprogram from theudfcli.sqcsource file insqllib\samples\cusing the batch filebldapp.You can build the embedded SQL C++
udfcliprogram from theudfcli.sqxsource file insqllib\samples\cppusing the batch filebldapp.To run the UDF, enter:udfcliThe calling application calls the
ScalarUDFfunction from theudfsrvDLL.
- Embedded SQL UDF DLL
- To build the embedded SQL user-defined function library
udfemsrvfrom the C source fileudfemsrv.sqcinsqllib\samples\c, or from the C++ source fileudfemsrv.sqxinsqllib\samples\cpp, enter:
If connecting to another database, also enter the database name:bldrtn udfemsrvbldrtn udfemsrv databaseThe 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 buildudfemclifrom the C source fileudfemcli.sqcinsqllib\samples\c, or from the C++ source fileudfemcli.sqxinsqllib\samples\cppusing the batch filebldapp.To run the UDF, enter:udfemcliThe calling application calls the UDFs in the
udfemsrvDLL.