DB2 UDB V8.1 Family Application Development Certification:: User-Defined Routines
DB2 UDB V8.1 Family Application Development Certification:: User-Defined Routines
DB2 UDB V8.1 Family Application Development Certification:: User-Defined Routines
08 Apr 2004
In this tutorial, you'll learn about using user-defined routines with the DB2 Universal
Database. You'll see how you can utilize programming logic within DB2 to simplify
and speed up your applications. This is the last tutorial in a series of seven that you
can use to help prepare for the DB2 UDB V8.1 Family Application Development
Certification exam (Exam 703).
User-defined routines
© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 1 of 47
developerWorks® ibm.com/developerWorks
You do not need a copy of DB2 Universal Database to complete this tutorial.
However, you can download a free trial version of IBM DB2 Universal Database from
the developerWorks downloads site for reference.
This tutorial is one of the tools that can help you prepare for Exam 703. You should
also review Resources at the end of this tutorial for more information.
This tutorial is designed to help you study for the user-defined routines section of the
DB2 Family Application Development exam. For this tutorial to be useful, you should
have a solid understanding of how SQL is used and how it works with a database.
An understanding of programming methodologies and flow of control usage would
also help. You should also have a base knowledge of:
• Databases
• Database objects
• Application compilation
Terminology review
Before you begin this tutorial, you need to be familiar with the concept of an SQL
access plan. This is a set of steps the DB2 engine uses to execute an SQL
statement. It includes the indexes that are used, the times at which fields are
retrieved from the database tables, and the order in which the steps of the query are
executed. The access plan is created by a database engine based on an SQL
statement sent to that engine.
We'll be using the term throughout this tutorial, so it's important to make sure you
understand it up front.
IBM, DB2, DB2 Universal Database, DB2 Information Integrator, WebSphere and
WebSphere MQ are trademarks or registered trademarks of IBM Corporation in the
United States, other countries, or both.
User-defined routines
Page 2 of 47 © Copyright IBM Corporation 1994, 2007. All rights reserved.
ibm.com/developerWorks developerWorks®
Other company, product, and service names may be trademarks or service marks of
others.
Traditionally, people think of a function as a program that takes in some inputs and
then returns a result. Within a database, you have a great deal more flexibility and
possibilities in what you can do with a function. There are a number of different types
of functions, and also different languages you can write them in.
Function types
There are two distinct types of functions. The more common type is an internal
function, which is a function written using SQL statements. Functions can also be
written as references to external components of code, be they in the Java language,
User-defined routines
© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 3 of 47
developerWorks® ibm.com/developerWorks
One important difference between the two types of functions is the location at which
you compile them. An SQL function can be compiled either remotely or locally, since
it will be stored in the database server's catalog tables. An external function,
however, is not as flexible. It must be compiled on the server, since the location of
the compiled code is then stored by the database server. If you compile it on a client,
then the code is stored on the client. The local client would be able to execute the
function, since the DB2 client would be able to find the program. However, the
server or any other clients would return an error, since the compiled code would not
be accessible to them. You could compile the function on the client and copy it to the
server at the exact same location to avoid this problem. But for that to work, the
operating system and underlying hardware need to be the same on both systems,
since the code would have been compiled by a specific compiler for the client
system and may not work with another computer/OS combination.
Creating a function
Internal functions are easy to create and use in your applications. A simplified
version of the syntax diagram is displayed below and is followed by an explanation
of its major parts.
>>-CREATE FUNCTION--function-name------------------------------->
>--(--+--------------------------------+--)--*------------------>
| .-,--------------------------. |
| V | |
'---parameter-name--data-type1-+-'
>--RETURNS--+-data-type2-----------------+--*------------------->
'-+-ROW---+--| column-list |-'
'-TABLE-'
.-LANGUAGE SQL-.
>--+-------------------------+--*--+--------------+--*---------->
'-SPECIFIC--specific-name-'
.-NOT DETERMINISTIC-. .-EXTERNAL ACTION----.
>--+-------------------+--*--+--------------------+--*---------->
'-DETERMINISTIC-----' '-NO EXTERNAL ACTION-'
.-READS SQL DATA---------.
>--+------------------------+--*--+-----------------+--*-------->
+-CONTAINS SQL-----------+
| |
'-MODIFIES SQL DATA------'
>--| SQL-function-body |--------------------------------------->
column-list:
.-,-----------------------.
V |
|--(----column-name--data-type3-+--)----------------------------|
SQL-function-body:
|--+-RETURN Statement-----------+-------------------------------|
User-defined routines
Page 4 of 47 © Copyright IBM Corporation 1994, 2007. All rights reserved.
ibm.com/developerWorks developerWorks®
'-dynamic-compound-statement-'
The major components of an SQL function are listed below. We'll look at some
examples on the next panel.
RETURNS
The RETURNS specification controls the type of function that will be created. The
three major types are scalar, row, and table. A scalar function, like the following
example, only returns a single data type value:
User-defined routines
© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 5 of 47
developerWorks® ibm.com/developerWorks
A row function, like the following example, separates a user-defined type into its
different components:
A table function, like the following example, will return zero or more rows of a table.
The table can be created from SQL or from application logic.
SPECIFIC
The SPECIFIC command is used to ensure that you have a specific name for the
function that allows you to remember what the compiled function is. This specific
name can then be referenced when you are sourcing, dropping, or commenting the
function. This is useful when you have overloaded the function and have more than
one version of the function with different data inputs.
The following two examples illustrate such overloading. The first function joins two
integer values by adding them together. The second joins the string new_ to the
input string.
User-defined routines
Page 6 of 47 © Copyright IBM Corporation 1994, 2007. All rights reserved.
ibm.com/developerWorks developerWorks®
LANGUAGE SQL
CONTAINS SQL
NO EXTERNAL ACTION
DETERMINISTIC
RETURN 'new_' || x;
DETERMINISTIC
The DETERMINISTIC option is used to specify whether the function always returns
the same value. This information can then be used by DB2 to optimize how the
function is called, since DB2 would already know the value of the function if it had
been run once before and is deterministic. If a function uses any special registers, or
it calls any nondeterministic functions, it must be nondeterministic.
EXTERNAL ACTION
This option specifies whether the function will change any objects outside the
database system. This option must be set to EXTERNAL ACTION if the function calls
any functions that have an external impact. An example would be a function that
modifies a flat file or alters data in an external source, such as a genome data file.
HAS SQL
This option lets DB2 know how the function is going to interact with the database.
You have a number of options here:
• CONTAINS SQL: Indicates that SQL statements that neither read nor
modify SQL data can be executed by the function.
• READS SQL DATA: Indicates that SQL statements that do not modify
SQL data can be executed by the function.
User-defined routines
© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 7 of 47
developerWorks® ibm.com/developerWorks
Function rules
There are a number of restrictions on functions that you should be aware of when
dealing with DB2 SQL functions:
You are no longer limited to a single line of code for your SQL function logic. A
compound SQL statement is a group of statements contained within a BEGIN-END
block. The SQL statements are then treated as one atomic unit. This block of
statements can be dynamically prepared.
>>-+-------------+--BEGIN ATOMIC-------------------------------->
>--+-----------------------------------------+------------------>
| .-------------------------------------. |
| V | |
'---+-| SQL-variable-declaration |-+--;-+-'
'-| condition-declaration |----'
.-,--------------------------.
V |
User-defined routines
Page 8 of 47 © Copyright IBM Corporation 1994, 2007. All rights reserved.
ibm.com/developerWorks developerWorks®
>----SQL-procedure-statement--;-+--END--+-------+-------------->
SQL-variable-declaration:
.-,-----------------.
V |
|--DECLARE----SQL-variable-name-+--data-type-------------------->
.-DEFAULT NULL------------.
>--+-------------------------+----------------------------------|
'-DEFAULT--default-values-'
condition-declaration:
|--DECLARE--condition-name--CONDITION--FOR---------------------->
.-VALUE-.
.-SQLSTATE--+-------+-.
>--+---------------------+--string-constant---------------------|
DECLARE: This allows you to declare variables within an SQL block. The data type
can be any user-defined type or standard SQL data type. If a default value is not
given for a data type, it is automatically set to null when it is declared. Here are
some examples:
SQL control statements: The following statements can be used within a compound
statement:
• FOR
• GET DIAGNOSTICS
• IF
• ITERATE
• LEAVE
• SIGNAL
• WHILE
More information can be found on these statements in the DB2 online help.
User-defined routines
© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 9 of 47
developerWorks® ibm.com/developerWorks
• Searched UPDATE
• Searched DELETE
• INSERT
• SET variable statement
Working with stored procedures covers the different parts of SQL compound
statements and provides some more detailed examples. There are a number of
differences between the use of procedural SQL in functions and in procedures.
Scalar functions
The different function types offer you different ways to return data to your SQL
statements. It is important to understand how the function types can be used, and
also how easily complex functions can be written.
An SQL scalar function is a traditional function that is probably the first type you
think of when you hear the word function. It returns a single value in any of the data
types within DB2. This function is normally constructed using an SQL statement.
The simple example below illustrates how you can hide the logic of your programs in
a function. The function changeSal is created with a single line of real code:
RETURN sal * 2. The remainder of the lines are the function definition. The
function takes in as input the salary figure of an employee, or any other double field.
It also works with other number values, since DB2 will implicitly cast them for you.
SELECT empno,
changeSal(salary) as newSalary
FROM employee
WHERE edlevel > 12;
Result from the DB2 sample database:
EMPNO NEWSAL
------ ----------------------
000030 87975.00
Scalar functions are normally more complicated than this, though, and can involve
full SQL statements. Let's create a more complex function:
User-defined routines
Page 10 of 47 © Copyright IBM Corporation 1994, 2007. All rights reserved.
ibm.com/developerWorks developerWorks®
SELECT edLevel,
edCount(edLevel)
FROM employee
GROUP BY edlevel;
DB2 takes the SQL function and in-line it into the SQL statement in which the
function is called. When you in-line a function, the function call in the calling SQL
statement is actually replaced by the function text. As a result, the DB2 optimizer
can then create an optimized access plan for the entire statement and not just a
section of it. This leads to better overall access plans.
SELECT empno,
changeSal(salary) as newSalary
FROM employee
WHERE edlevel > 12;
SELECT empno,
sal * 2 as newSalary
FROM employee
WHERE edlevel > 12;
The second example's in-lining is much more complicated despite how simple the
initial SQL statement appears. This example clearly illustrates the benefit of moving
logic into the function instead of your own SQL statements. Here's the original SQL
statement again:
SELECT edLevel,
edCount(edLevel)
FROM employee
GROUP BY edLevel;
User-defined routines
© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 11 of 47
developerWorks® ibm.com/developerWorks
(SELECT Q1.EDLEVEL
FROM DB2ADMIN.EMPLOYEE AS Q1) AS Q2
GROUP BY Q2.$C0) AS Q3,
(SELECT COUNT(* )
FROM
(SELECT $RID$
FROM DB2ADMIN.EMPLOYEE AS Q4
WHERE (Q4.EDLEVEL = DOUBLE(Q3.$C0))) AS Q5)AS Q6
Row functions
With functions, you can also return an entire row of data, instead of just a single data
type. The row function does not return a row of data, as you might expect from its
name. It is instead used to transform structured data types into their individual
components. User-defined types, or UDTs, are data types defined by the user that
contain references to one or more DB2 data types. (UDTs are covered in detail in
the sixth tutorial in this series, see Resources.) You would therefore only use row
functions if you have UDTs in your database. Row functions can only be defined as
SQL functions, and you cannot use the other programming languages that are
normally allowed for DB2 functions.
The following example illustrates how you can use a row function to determine the
last name and first name of a person. The person object in the example is a UDT
that contains both the lastName and firstName fields. Our function can be used
to break apart the information you need from the person type.
User-defined routines
Page 12 of 47 © Copyright IBM Corporation 1994, 2007. All rights reserved.
ibm.com/developerWorks developerWorks®
Table functions
One of the most powerful aspects of DB2 functions is their ability to return an entire
table of data instead of a single value. This opens up many sources of information
that you can use in your SQL statements. Instead of referring to a database table,
you could instead write a C function that referred to a live data stream such as stock
market data.
Table functions are actually quite easy to write and to reference. Instead of
referencing a single data value, as in scalar functions, you reference multiple
variables that refer to the different columns that will be returned in the table data, as
in the following example:
There are restrictions to this: You must know the data types and number of
parameters that your SQL table will return. If you have a function that refers to the
entire table's columns and you add an additional column, then your function may not
work properly. For instance, suppose we create the following table:
User-defined routines
© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 13 of 47
developerWorks® ibm.com/developerWorks
Our function call will no longer return the expected value of all three columns, but will
instead return just the columns that were defined when it was created. This is
because the '*' used in the function definition is expanded out at creation time and
not at execution. Here's the SQL output for the modified table:
In order to be able to create an external function, you must have at least one of the
following privileges:
User-defined routines
Page 14 of 47 © Copyright IBM Corporation 1994, 2007. All rights reserved.
ibm.com/developerWorks developerWorks®
The functions can run either unfenced -- that is, inside the database engine -- or
fenced -- that is, in memory outside of the database engine. The advantage to
having an unfenced function is that it shares the memory area with the engine and
can communicate it with it more quickly. However, if an unfenced function has not
been written properly, a memory leak may occur. If this happens, the memory leak
could start overwriting the memory associated with the DB2 engine, which could
have horrible repercussions. If you are writing C or C++ functions it is highly
recommended that you write them as fenced.
To create an unfenced procedure, you will need one of the following privileges:
>>-CREATE FUNCTION--function-name------------------------------->
>--(--+----------------------------------------------------+--)-->
| .-,----------------------------------------------. |
| V | |
'---+----------------+--data-type1--+------------+-+-'
'-parameter-name-' '-AS LOCATOR-'
>--*------------------------------------------------------------>
>--RETURNS--+-data-type2--+------------+------------------------+-->
| '-AS LOCATOR-' |
'-data-type3--CAST FROM--data-type4--+------------+-'
'-AS LOCATOR-'
>--*--+-------------------------+--*---------------------------->
'-SPECIFIC--specific-name-'
>--EXTERNAL--+----------------------+--*------------------------>
'-NAME--+-'string'---+-'
'-identifier-'
>--LANGUAGE--+-C----+-------*----------------------------------->
+-JAVA-+
'-OLE--'
.-NOT DETERMINISTIC-.
>--PARAMETER STYLE--+-DB2GENERAL-+--*--+-------------------+---->
+-JAVA-------+ '-DETERMINISTIC-----'
'-SQL--------'
.-FENCED------------------------.
>--*--+-------------------------------+--*---------------------->
+-FENCED--*--+-THREADSAFE-----+-+
| '-NOT THREADSAFE-' |
| .-THREADSAFE-. |
'-NOT FENCED--*--+------------+-'
.-RETURNS NULL ON NULL INPUT-. .-READS SQL DATA-.
>--+----------------------------+--*--+----------------+--*----->
'-CALLED ON NULL INPUT-------' +-NO SQL---------+
'-CONTAINS SQL---'
.-EXTERNAL ACTION----.
User-defined routines
© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 15 of 47
developerWorks® ibm.com/developerWorks
>--+-----------------+--*--+--------------------+--*------------>
'-NO EXTERNAL ACTION-'
.-NO SCRATCHPAD----------. .-NO FINAL CALL-.
>--+------------------------+--*--+---------------+--*---------->
| .-100----. | '-FINAL CALL----'
'-SCRATCHPAD--+--------+-'
'-length-'
.-NO DBINFO-.
>--+-------------------+--*--+-----------+--*------------------->
+-ALLOW PARALLEL----+ '-DBINFO----'
'-DISALLOW PARALLEL-'
• Language: This identifies the language that the function is written in.
• Parameter style: This clause is used to specify the conventions used for
passing parameters to and returning the value from functions.
• External action: This determines if external actions can be performed by
the function.
• Scratchpad: The scratchpad is used as a memory area where data can
be stored by the function.
• Final call: This is used to deallocate memory that is being utilized by the
function.
C functions
>>-'--+-library_id-------+--+------------+--'------------------>|
'-absolute_path_id-' '-!--func_id-'
• library_id: This is the location of the library containing the function. The
database manager will look for the library in the .../sqllib/function
directory (on UNIX-based systems) or the
...\sqllib\instance_name\function directory (on Windows
User-defined routines
Page 16 of 47 © Copyright IBM Corporation 1994, 2007. All rights reserved.
ibm.com/developerWorks developerWorks®
>>-'--+----------+--class_id--+-.-+--method_id--'-------------->|
'-jar_id :-' '-!-'
• jar_id: Identifies the JAR identifier given to the JAR collection when it was
installed in the database. It can be either a simple identifier, such as
myJar, or a schema-qualified identifier, such as mySchema.myJar.
• class_id: Identifies the class identifier of the Java object. If the class is
part of a package, the class identifier part must include the complete
package prefix. For example, if you use myPacks.UserFuncs, the Java
Virtual Machine will look in the directory .../myPacks/UserFuncs/ (or
...\myPacks\UserFuncs\ on Windows) for the classes.
• method_id: Identifies the method name of the Java object to be invoked.
OLE functions
>>-'--+-progid-+--!--method_id--'------------------------------>|
'-clsid--'
User-defined routines
© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 17 of 47
developerWorks® ibm.com/developerWorks
Now let's try a Java language example. The following example calls the findc
function. No SQL is called in the function.
Binding a function
If you create an external function that executes SQL, you will have to PREPARE and
BIND the application that will be connected to the database.
The PREPARE command checks that the embedded static SQL in your application is
properly formed. For more details on this command, check the DB2 online help
under "PRECOMPILE Command."
The BIND command invokes the bind utility, which prepares SQL statements stored
in the bind file generated by the precompiler and creates a package that is stored in
the database. Further information on this step can be found in the DB2 online help
under "BIND Command."
User-defined routines
Page 18 of 47 © Copyright IBM Corporation 1994, 2007. All rights reserved.
ibm.com/developerWorks developerWorks®
Let's look at an example. The following C program retrieves the application ID for the
application that calls it. The program simply retrieves the appl_id field from the
dbinfo data structure. This information cannot be extracted using SQL, so the
function can be useful. Here's the C code
#include <string.h>
#include <sqludf.h>
void SQL_API_FN getApplicationId(
SQLUDF_CHAR *applId, SQLUDF_NULLIND *applId_ind,
SQLUDF_TRAIL_ARGS, SQLUDF_DBINFO *dbinfo)
{
strncpy(applId, dbinfo->appl_id, 128);
*applId_ind = 0;
}
To transform this function from C code to a state where it can be called in SQL, you
need to walk through the following steps:
@ echo off
rem ------------------------------------------
rem Script to create the C function
rem crFunc.bat databaseName [user [pwd]]
rem ------------------------------------------
rem Default compiler is set to Microsoft Visual C++
rem Microsoft C/C++ Compiler
set BLDCOMP=cl
set PROG=application_Id
rem call sdkvars
rem Precompile and bind the program.
rem Connect to a database.
if "%1" == "" goto error
if "%2" == "" goto case1
User-defined routines
© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 19 of 47
developerWorks® ibm.com/developerWorks
Once you've run this script, the function can be created in the database:
User-defined routines
Page 20 of 47 © Copyright IBM Corporation 1994, 2007. All rights reserved.
ibm.com/developerWorks developerWorks®
The function can now be called in any SQL statement, like so:
VALUES(application_id())
The stored procedures covered in this section are all written using the DB2 SQL
Procedure Language, known as SQL PL. Stored procedures can be written in a
variety of languages, including C, the Java language, and OLE, however, it is easier
to illustrate how they can be used if simple SQL language examples are used
instead. This is the language we'll use in this section.
• Reduced network traffic: Because the stored procedures send only the
User-defined routines
© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 21 of 47
developerWorks® ibm.com/developerWorks
procedure call and the final result across the network, not all of the interim
data sets need to be sent back to the client. For applications where the
client and the server are geographically distant, this can dramatically
improve performance.
• Improved performance: The stored procedures will be executed on the
server instead of on the client. In the vast majority of architectures, the
server is a powerful machine that can process the SQL much quicker than
the client can.
• Reduced complexity: By placing all of the database application code in
modular units, you have isolated the application's interaction with the
database to a single layer. This allows for easier testing and debugging
as well as maintenance.
• Division of responsibilities: Having all of the data interaction isolated to
the stored procedures allows the interactions to be controlled by the DBA.
The DBA is the one who knows the system best, and he or she will be
able to respond the quickest to any performance problems.
• Ease of use: Programming applications that process data using stored
procedures is quite simple and allows business logic to be easily
translated into application logic. By leaving the application in the data
format that accesses the database, less application programming skill is
needed.
• Stored procedures can return any DB2 system data type. The return
value of the stored procedure, however, can only be an integer.
• A stored procedure cannot be called within an SQL statement.
• Stored procedures cannot be called within triggers or functions.
• The length of a procedure is limited to 64 KB of text.
Unfortunately, the workaround for using stored procedure logic in functions and
triggers is to embed the entire stored procedure logic in the function or trigger.
Stored procedures are most efficient when they are used to encapsulate a large
section of related logic. You should treat your stored procedures just like application
programs. If a section of code is repeated often throughout your application or
procedures, then it probably makes sense to encapsulate it into a stored procedure.
However, if the logic is only called once, then it makes sense to leave it as code.
You should also avoid writing very large stored procedures that perform multiple
tasks, unless the stored procedure is clearly defined as doing so. You may want to
User-defined routines
Page 22 of 47 © Copyright IBM Corporation 1994, 2007. All rights reserved.
ibm.com/developerWorks developerWorks®
put all of your database logic into a stored procedure. If you have a simple call to the
database to return a result set, then creating a stored procedure for this task would
be overkill. It would be better to open a cursor directly within your application.
You can use existing free or open source compilers for compiling the stored
procedures. The two most common compilers used are the GCC compiler and the
trial edition of the Microsoft Visual C++ compiler (see Resources). Microsoft
currently has a trial version of its compiler available that can be used for compiling
stored procedures. This is for development purposes only. A full license must be
purchased if you are going to use it in production.
Creating a procedure
Once you have a compiler installed, you are ready to create a procedure. The syntax
is similar to that for creating a function.
>>-CREATE PROCEDURE--procedure-name----------------------------->
>--+----------------------------------------------------+--*---->
'-(--+------------------------------------------+--)-'
| .-,------------------------------------. |
| V .-IN----. | |
'---+-------+--parameter-name--data-type-+-'
+-OUT---+
'-INOUT-'
>--+-------------------------+--*------------------------------->
'-SPECIFIC--specific-name-'
.-DYNAMIC RESULT SETS 0--------. .-MODIFIES SQL DATA-.
>--+------------------------------+--*--+-------------------+--->
'-DYNAMIC RESULT SETS--integer-' +-CONTAINS SQL------+
'-READS SQL DATA----'
.-NOT DETERMINISTIC-.
>--*--+-------------------+--*----------------------------*----->
'-DETERMINISTIC-----'
.-LANGUAGE SQL-.
>--+----*--+--------------+--*-------->
>--| SQL-procedure-body |-------------------------------------->|
SQL-procedure-body:
|--SQL-procedure-statement--------------------------------------|
User-defined routines
© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 23 of 47
developerWorks® ibm.com/developerWorks
• Parameter type: There are three parameter types. IN is used for input
parameters. Changes to these parameters are not passed back to the
calling application. OUT is used for output parameters. Changes to these
parameters are passed back out to the calling application. INOUT is used
for input and output. Changes to these parameters and their input values
affect the stored procedure and the calling application.
• Specific: A specific name can be assigned to label a stored procedure
internally. DB2 assigns a system-generated value if a name is not
specified.
• Dynamic Result Sets: Specifies the number of results sets that will be
left open and passed back to the calling program or application.
• Deterministic: Specifies if the stored procedure will return the same
result set if the same input is given.
• Language: Currently, only the DB2 SQL PL language is supported for
SQL procedures.
• The SQL data access indication of the statement from the following table.
• The SQL data access indication of the routine specified when the routine
was created.
The table below is taken from the DB2 documentation:
User-defined routines
Page 24 of 47 © Copyright IBM Corporation 1994, 2007. All rights reserved.
ibm.com/developerWorks developerWorks®
User-defined routines
© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 25 of 47
developerWorks® ibm.com/developerWorks
SELECT INTO N N Y Y
SET N N N N
CONNECTION
SET INTEGRITY N N N Y
SET special N Y Y Y
register
UPDATE N N N Y
VALUES INTO N N Y Y
WHENEVER Y Y Y Y
This syntax will not necessarily work for your stored procedure, depending on how
line endings are handled. The different options for the db2 command that are used
with running scripts are:
User-defined routines
Page 26 of 47 © Copyright IBM Corporation 1994, 2007. All rights reserved.
ibm.com/developerWorks developerWorks®
If the script is compiled using the command db2 -tvf fileName, DB2 will stop at
(1) and treat it as one command, resulting in an error. To avoid this problem, you
have to compile it with the command db2 -td@ -f fileName. DB2 will then treat
the ; character at (1) as an end-of-line character and continue processing the
procedure. Only when the @ character is reached at (2) will DB2 stop processing it
as one entire procedure.
The second example is more complex and expands on the first. It checks how many
employees are in a work department and gives them a larger budget if they have
less than four people.
User-defined routines
© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 27 of 47
developerWorks® ibm.com/developerWorks
The Development Center is included with DB2 as a free tool. You can run it by
selecting it from the IBM DB2 options in the Windows Start Menu:
You can also run it from within any of the other DB2 GUI tools, such as the DB2
Control Center.
User-defined routines
Page 28 of 47 © Copyright IBM Corporation 1994, 2007. All rights reserved.
ibm.com/developerWorks developerWorks®
If you are going to be using the DC from a UNIX or Linux client, then you must have
the X Window System or equivalent software installed to ensure that the GUI will be
displayed.
Setting up a project
When you first open the Development Center, you will be asked to create a project.
The project stores all the information about the databases you have connections to,
the procedures you are working on, and the status of your project. Previously, the
Stored Procedure Builder would display all of the procedures that you had in the
database. By being able to pick which ones you want to work with, projects can be
handled much more clearly.
User-defined routines
© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 29 of 47
developerWorks® ibm.com/developerWorks
The project needs an initial connection to a database. The list of databases provided
to you is taken from the databases that you have catalogued on your local client. If
the required database is not in the list, you can add it by selecting the Add option.
This catalogues the new database on the client.
User-defined routines
Page 30 of 47 © Copyright IBM Corporation 1994, 2007. All rights reserved.
ibm.com/developerWorks developerWorks®
Creating an object
An SQL PL procedure isn't the only kind that you can build in the Development
Center. You can also work with Java procedures, though only in a project working
with user-defined functions. We'll cover the use of Java procedures in Advanced use
of the Development Center .
User-defined routines
© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 31 of 47
developerWorks® ibm.com/developerWorks
If you want to build a new procedure, the DC walks you through all of the options
step by step. The Object Building wizard is simple enough that even novices to SQL
procedures can build a procedure, but also complex enough that experts can build
fully functional stored procedures. The figure below shows the different options for
the header of the stored procedure.
User-defined routines
Page 32 of 47 © Copyright IBM Corporation 1994, 2007. All rights reserved.
ibm.com/developerWorks developerWorks®
You should also give the procedure a specific name in case any overloading is used.
(The use of specific names with stored procedures is discussed in Creating a
procedure.)
User-defined routines
© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 33 of 47
developerWorks® ibm.com/developerWorks
• Project view: Lists all of projects that are currently open, the databases
they have connections to, and the stored procedures that are part of a
particular project.
• Server view: Summarizes the items based on database connections and
includes information on stored procedures, triggers, functions, tables, and
views.
• Output view: Lists the status of the actions you have just completed or
are working on.
• Editor view: Displays objects for editing.
The following figure shows you an overall picture of the Development Center
interface.
User-defined routines
Page 34 of 47 © Copyright IBM Corporation 1994, 2007. All rights reserved.
ibm.com/developerWorks developerWorks®
• Outside of the DC: The view can be dragged outside the DC to become
a stand-alone object. This is useful for viewing a list of objects in a
separate frame, which reduces the clutter of the Development Center.
• Tab: The views can also be lined up as tabs. All of the information can be
quickly viewed but still remain hidden when you don't need to see it.
Selecting this option can be a little tricky. You have to select the view with
the hand icon and move it over to the edge of another view. Move the
object around the edge until you see an object appear that resembles a
set of file tabs. You can then let go and the views will be combined into a
set of tabs. Two or more views can be combined this way.
User-defined routines
© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 35 of 47
developerWorks® ibm.com/developerWorks
User-defined routines
Page 36 of 47 © Copyright IBM Corporation 1994, 2007. All rights reserved.
ibm.com/developerWorks developerWorks®
The individual icons used for building and debugging the procedures are covered in
the following panels.
Building a procedure
There are two main modes that the stored procedures can be built in: normal build
and build for debug. The build for debug option allows you to debug the procedure. It
automatically inserts all the required breakpoints and step-through information into
the procedure. It is therefore recommended that for a production environment you
ensure that your stored procedures have been built in normal mode. The debug
mode significantly slows the performance of the procedures.
• Build (1): This command builds the stored procedure in regular mode.
• Run (2): This command executes the stored procedure.
• Build in Debug (3): This command builds the stored procedure in debug
mode.
User-defined routines
© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 37 of 47
developerWorks® ibm.com/developerWorks
User-defined routines
Page 38 of 47 © Copyright IBM Corporation 1994, 2007. All rights reserved.
ibm.com/developerWorks developerWorks®
Stored procedures can be developed using both SQLJ (static JDBC) or standard
Java code using JDBC.
User-defined routines
© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 39 of 47
developerWorks® ibm.com/developerWorks
A Java procedure can also be run normally as if it were an SQL PL procedure. The
dual run and debug capabilities of the DC allow you to use it for all your stored
procedure testing needs.
User-defined routines
Page 40 of 47 © Copyright IBM Corporation 1994, 2007. All rights reserved.
ibm.com/developerWorks developerWorks®
The list of languages that the Development Center supports is limited to a subset of
the total available languages for UDFs. If you want to develop UDFs in other
languages, you have to obtain the appropriate IDE and follow the steps laid out in
Using user-defined functions .
User-defined routines
© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 41 of 47
developerWorks® ibm.com/developerWorks
Once the provider is selected, you'll need to provide your user ID and password for
the connection. This information is used to build the connection string for the OLE
function.
User-defined routines
Page 42 of 47 © Copyright IBM Corporation 1994, 2007. All rights reserved.
ibm.com/developerWorks developerWorks®
having the DC quickly create a base template for you to fill in.
The UDF editing window is similar to the procedure editor, but you no longer have
the debugging option.
Section 7. Conclusion
User-defined routines
© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 43 of 47
developerWorks® ibm.com/developerWorks
Summary
This tutorial covered how functions and stored procedures can be used in your
application. User-defined functions can be used in a variety of environments and
with multiple languages. Both SQL and external programs can be used to build
complex functions that can be integrated into your SQL statements.
The discussion of stored procedures and functions was all tied together in the
overview of the DB2 Development Center. The Development Center is a leap
forward from the original stored procedure IDE, the Stored Procedure Builder. The
DC allows you to build and debug stored procedures written in both SQL PL and the
Java language. It now has the capacity to build user-defined functions written in SQL
PL and a variety of external languages as well. You can use projects to contain all of
your database objects, procedures, and functions into one modular unit. This new
packaging in the DC will help you build your applications with DB2.
User-defined routines
Page 44 of 47 © Copyright IBM Corporation 1994, 2007. All rights reserved.
ibm.com/developerWorks developerWorks®
Resources
Learn
• This tutorial will help you study for the application development exam, but there
is only so much space and time to explain the many concepts. Each of the
sections has information at the end that refers to you to keywords that you can
look up in the DB2 documentation for further information. The best source of
information for DB2 is the new Information Center. This Information Center was
added in DB2 V8.1 Fixpak 4 and has an entirely new interface written using the
Eclipse programming framework. It is quick and easy to use, and the search
utility built into it is far better than any of the previous incarnations. It also has
links to other sources of DB2 information on the Web, such as Google and the
DB2 Technotes. >All of my references at the end of the sections refer to this
source and not to the DB2 documentation included with the product itself. The
new Information Center is written by the DB2 Information Development team
and is the new official reference for DB2 help. Go out and try it! The DB2
Information Center is also available on the Web.
• There is an excellent and in-depth reference on stored procedures available. I
am slightly biased, since I am one of the authors but I still recommend it to
anyone who has to work extensively with procedures: DB2 SQL Procedural
Language for Linux, Unix and Windows, Yip, Bradstock, Curtis, Gao,
Janmohamed, Liu, and McArthur (Prentice Hall, 2002).
• In Creating a stored procedure, we discussed the fact that you need a C
compiler on your system in order to compile stored procedures. There are
several options available to you:
• GCC compiler
• Microsoft is offering a free .Net compiler (and other .Net development
tools) on its Web site. There are two products you need in order to use
these compilers. (They also must be installed in the order presented here.)
The first is the .Net Framework Version 1.1 Redistributable Package; the
second is the .Net Framework SDK Version 1.1.
• For more information on the DB2 UDB V8.1 Family Application Development
Certification (Exam 703), see IBM DB2 Information Management -- Training and
certification for information on classes, certifications available and additional
resources.
• As mentioned earlier, this tutorial is just one tutorial in a series of seven to help
you prepare for the DB2 UDB V8.1 Family Application Development
Certification exam (Exam 703). The complete list of all tutorials in this series is
provided below:
2. Data Manipulation
User-defined routines
© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 45 of 47
developerWorks® ibm.com/developerWorks
4. ODBC/CLI Programming
5. Java Programming
6. Advanced Programming
7. User-Defined Routines
• Before you take the certification exam (DB2 UDB V8.1 Application
Development, Exam 703) for which this tutorial was created to help you
prepare, you should have already taken and passed the DB2 V8.1 Family
Fundamentals certification exam (Exam 700). Use the DB2 V8.1 Family
Fundamentals certification prep tutorial seriesto prepare for that exam. A set of
six tutorials covers the following topics:
• DB2 planning
• DB2 security
• Accessing DB2 UDB data
• Working with DB2 UDB data
• Working with DB2 UDB objects
• Data concurrency
• Use the DB2 V8.1 Database Administration certification prep tutorial seriesto
prepare for the DB2 UDB V8.1 for Linux, UNIX and Windows Database
Administration certification exam (Exam 701). A set of six tutorials covers the
following topics:
• Server management
• Data placement
• Database access
• Monitoring DB2 activity
• DB2 utilities
• Backup and recovery
User-defined routines
Page 46 of 47 © Copyright IBM Corporation 1994, 2007. All rights reserved.
ibm.com/developerWorks developerWorks®
User-defined routines
© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 47 of 47