Creating external stored procedures
An external stored procedure is a procedure that is written in a host language and can contain SQL statements. The source code for external procedures is separate from the definition.
Before you begin
Before you create an external procedure, Configure Db2 for running stored procedures and user-defined functions during installation or Configure Db2 for running stored procedures and user-defined functions during migration.
About this task
Restriction: These instructions
do not apply to Java™ stored
procedures. The process for creating a Java stored procedure is
different. The preparation process varies depending on what the procedure
contains.
Procedure
To create an external stored procedure:
Example of defining a C stored procedure
Suppose
that you have written and prepared a stored procedure that has the
following characteristics:
- The name of the stored procedure is B.
- The stored procedure has the following two parameters:
- An integer input parameter that is named V1
- A character output parameter of length 9 that is named V2
- The stored procedure is written in the C language.
- The stored procedure contains no SQL statements.
- The same input always produces the same output.
- The load module name is SUMMOD.
- The package collection name is SUMCOLL.
- The stored procedure is to run for no more than 900 CPU service units.
- The parameters can have null values.
- The stored procedure is to be deleted from memory when it completes.
- The stored procedure needs the following Language Environment® runtime
options:
MSGFILE(OUTFILE),RPTSTG(ON),RPTOPTS(ON)
- The stored procedure is part of the WLM application environment that is named PAYROLL.
- The stored procedure runs as a main program.
- The stored procedure does not access non-Db2 resources, so it does not need a special RACF® environment.
- The stored procedure can return at most 10 result sets.
- When control returns to the client program, Db2 does not commit updates automatically.
The following CREATE PROCEDURE statement defines the stored procedure to Db2:
CREATE PROCEDURE B(IN V1 INTEGER, OUT V2 CHAR(9))
LANGUAGE C
DETERMINISTIC
NO SQL
EXTERNAL NAME SUMMOD
COLLID SUMCOLL
ASUTIME LIMIT 900
PARAMETER STYLE GENERAL WITH NULLS
STAY RESIDENT NO
RUN OPTIONS 'MSGFILE(OUTFILE),RPTSTG(ON),RPTOPTS(ON)'
WLM ENVIRONMENT PAYROLL
PROGRAM TYPE MAIN
SECURITY DB2
DYNAMIC RESULT SETS 10
COMMIT ON RETURN NO;