The CREATE PROCEDURE (external) statement defines an external procedure at the current server.
This statement can be embedded in an application program or issued through the use of dynamic SQL statements. It is an executable statement that can be dynamically prepared only if DYNAMICRULES run behavior is in effect for the package (SQLSTATE 42509).
To create a fenced procedure, no additional authorities or privileges are required.
To replace an existing procedure, the authorization ID of the statement must be the owner of the existing procedure (SQLSTATE 42501).
>>-CREATE--+------------+--PROCEDURE--procedure-name------------> '-OR REPLACE-' >--+------------------------------------------------------------------------------------+--> '-(--+--------------------------------------------------------------------------+--)-' | .-,--------------------------------------------------------------------. | | V .-IN----. | | '---+-------+--+----------------+--| data-type |--+--------------------+-+-' +-OUT---+ '-parameter-name-' '-| default-clause |-' '-INOUT-' >--| option-list |--------------------------------------------->< data-type |--+-| built-in-type |-+----------------------------------------| '-array-type-name---' built-in-type |--+-+-SMALLINT----+----------------------------------------------------------------------+--| | +-+-INTEGER-+-+ | | | '-INT-----' | | | '-BIGINT------' | | .-(5,0)-------------------. | +-+-+-DECIMAL-+-+--+-------------------------+-----------------------------------------+ | | '-DEC-----' | | .-,0-------. | | | '-+-NUMERIC-+-' '-(integer-+----------+-)-' | | '-NUM-----' '-,integer-' | | .-(53)------. | +-+-FLOAT--+-----------+--+------------------------------------------------------------+ | | '-(integer)-' | | | +-REAL------------------+ | | | .-PRECISION-. | | | '-DOUBLE--+-----------+-' | | .-(1)------------------------. | +-+-+-+-CHARACTER-+--+----------------------------+----------+--+------------------+-+-+ | | | '-CHAR------' '-(integer-+-------------+-)-' | | (1) | | | | | | +-OCTETS------+ | '-FOR BIT DATA-----' | | | | | '-CODEUNITS32-' | | | | | '-+-VARCHAR----------------+--(integer-+-------------+-)-' | | | | '-+-CHARACTER-+--VARYING-' +-OCTETS------+ | | | | '-CHAR------' '-CODEUNITS32-' | | | | .-(1M)-----------------------------. | | | '-+-CLOB------------------------+--+----------------------------------+------------' | | '-+-CHARACTER-+--LARGE OBJECT-' '-(integer-+---+-+-------------+-)-' | | '-CHAR------' +-K-+ +-OCTETS------+ | | +-M-+ '-CODEUNITS32-' | | '-G-' | | .-(1)------------------------. | +-+-GRAPHIC--+----------------------------+------+-------------------------------------+ | | '-(integer-+-------------+-)-' | | | | +-CODEUNITS16-+ | | | | '-CODEUNITS32-' | | | +-VARGRAPHIC--(integer-+-------------+-)-------+ | | | +-CODEUNITS16-+ | | | | '-CODEUNITS32-' | | | | .-(1M)-----------------------------. | | | '-DBCLOB--+----------------------------------+-' | | '-(integer-+---+-+-------------+-)-' | | +-K-+ +-CODEUNITS16-+ | | +-M-+ '-CODEUNITS32-' | | '-G-' | | .-(1M)-------------. | +-+-BLOB----------------+--+------------------+----------------------------------------+ | '-BINARY LARGE OBJECT-' '-(integer-+---+-)-' | | +-K-+ | | +-M-+ | | '-G-' | +-+-DATE------+------------------------------------------------------------------------+ | +-TIME------+ | | '-TIMESTAMP-' | +-XML----------------------------------------------------------------------------------+ | .-SYSPROC.-. (2) (3) | '-+----------+--DB2SECURITYLABEL-------------------------------------------------------' default-clause |--DEFAULT--+-NULL-------------+--------------------------------| +-constant---------+ +-special-register-+ +-global-variable--+ '-(--expression--)-' option-list |--●--LANGUAGE--+-C-----+--●--+-------------------------+--●----> +-JAVA--+ '-SPECIFIC--specific-name-' +-COBOL-+ +-CLR---+ '-OLE---' .-DYNAMIC RESULT SETS 0--------. .-MODIFIES SQL DATA-. >--+------------------------------+--●--+-------------------+---> '-DYNAMIC RESULT SETS--integer-' +-NO SQL------------+ +-CONTAINS SQL------+ '-READS SQL DATA----' .-NOT DETERMINISTIC-. .-CALLED ON NULL INPUT-. >--●--+-------------------+--●--+----------------------+--●-----> '-DETERMINISTIC-----' .-OLD SAVEPOINT LEVEL-. >--+---------------------+--●-----------------------------------> '-NEW SAVEPOINT LEVEL-' >--EXTERNAL--+----------------------+--●------------------------> '-NAME--+-'string'---+-' '-identifier-' .-FENCED------------------------. >--+-------------------------------+--●-------------------------> +-FENCED--●--+-THREADSAFE-----+-+ | '-NOT THREADSAFE-' | | .-THREADSAFE-. | '-NOT FENCED--●--+------------+-' .-COMMIT ON RETURN NO--. >--+-+----------------------+-+--●------------------------------> | '-COMMIT ON RETURN YES-' | '-AUTONOMOUS---------------' .-EXTERNAL ACTION----. .-INHERIT SPECIAL REGISTERS-. >--+--------------------+--+---------------------------+--●-----> '-NO EXTERNAL ACTION-' >--PARAMETER STYLE--+-DB2GENERAL---------+--●-------------------> +-DB2SQL-------------+ +-GENERAL------------+ +-GENERAL WITH NULLS-+ +-JAVA---------------+ '-SQL----------------' >--+------------------------------+--●--------------------------> '-PARAMETER CCSID--+-ASCII---+-' '-UNICODE-' .-NO DBINFO-. >--+------------------------+--●--+-----------+--●--------------| '-PROGRAM TYPE--+-SUB--+-' '-DBINFO----' '-MAIN-'
The name, including the implicit or explicit qualifiers, together with the number of parameters must not identify a procedure described in the catalog (SQLSTATE 42723). The unqualified name, together with the number of the parameters, need not be unique across schemas.
If a two-part name is specified, the schema-name cannot begin with 'SYS' (SQLSTATE 42939).
No two identically-named procedures within a schema are permitted to have exactly the same number of parameters. A duplicate signature returns an SQL error (SQLSTATE 42723).
CREATE PROCEDURE PART (IN NUMBER INT, OUT PART_NAME CHAR(35)) ...
CREATE PROCEDURE PART (IN COST DECIMAL(5,3), OUT COUNT INT) ...
the
second statement will fail, because the number of parameters in the
procedure is the same, even if the data types are not.If an error is returned by the procedure, OUT parameters are undefined and INOUT parameters are unchanged.
The expression can be any expression of the type described in "Expressions". If a default value is not specified, the parameter has no default and the corresponding argument cannot be omitted on invocation of the procedure. The maximum size of the expression is 64K bytes.
The default expression must not modify SQL data (SQLSTATE 428FL or SQLSTATE 429BL). The expression must be assignment compatible to the parameter data type (SQLSTATE 42821).
The specific-name may be the same as an existing procedure-name.
If no qualifier is specified, the qualifier that was used for procedure-name is used. If a qualifier is specified, it must be the same as the explicit or implicit qualifier of procedure-name or an error (SQLSTATE 42882) is raised.
If specific-name is not specified, a unique name is generated by the database manager. The unique name is 'SQL' followed by a character timestamp: 'SQLyymmddhhmmssxxx'.
For the classification of each statement, see SQL statements that can be executed in routines and triggers.
The default is MODIFIES SQL DATA.
This clause currently does not impact processing of the procedure.
LANGUAGE OLE is only supported for procedures stored in databases running on Windows operating systems. THREADSAFE may not be specified for procedures defined with LANGUAGE OLE (SQLSTATE 42613).
If the NAME clause is not specified, "NAME procedure-name" is assumed. If the NAME clause is not formatted correctly, an error is returned (SQLSTATE 42878).
The 'string' option is a string constant with a maximum of 254 bytes. The format used for the string is dependent on the LANGUAGE specified.
The string specified is the library name and procedure within the library, which the database manager invokes to execute the procedure being CREATEd. The library (and the procedure within the library) do not need to exist when the CREATE PROCEDURE statement is performed. However, when the procedure is called, the library and procedure within the library must exist and be accessible from the database server machine.
>>-'--+-library_id-------+--+------------+--'------------------>< '-absolute_path_id-' '-!--proc_id-'
The name must be enclosed by single quotation marks. Extraneous blanks are not permitted.
Operating system | Library name location |
---|---|
Linux AIX® Solaris HP-UX |
If 'myfunc' was given as the library_id, and the database manager is being run from /u/production, the database manager will look for the procedure in library /u/production/sqllib/function/myproc if FENCED is specified, or /u/production/sqllib/function/unfenced/myproc if NOT FENCED is specified. |
Windows | The database manager will look for the function in a directory path that is specified by the LIBPATH or PATH environment variable. |
Stored procedures located in any of these directories do not use any of the registered attributes.
Operating system | Full path name example |
---|---|
Linux AIX Solaris HP-UX |
A value of '/u/jchui/mylib/myproc' would cause the database manager to look in /u/jchui/mylib for the myproc procedure. |
Windows | A value of 'd:\mylib\myproc.dll' would cause the database manager to load the file myproc.dll from the d:\mylib directory. If an absolute path ID is being used to identify the routine body, be sure to append the .dll extension. |
If the string is not properly formed, an error is returned (SQLSTATE 42878).
The body of every procedure should be in a directory that is mounted and available on every database partition.
The string specified contains the optional jar file identifier, class identifier and method identifier, which the database manager invokes to execute the procedure being created. The class identifier and method identifier do not need to exist when the CREATE PROCEDURE statement is performed. If a jar_id is specified, it must exist when the CREATE PROCEDURE statement is performed. However, when the procedure is called, the class identifier and the method identifier must exist and be accessible from the database server machine, otherwise an error is returned (SQLSTATE 42884).
>>-'--+----------+--class_id--+-.-+--method_id--'-------------->< '-jar_id :-' '-!-'
The name must be enclosed by single quotation marks. Extraneous blanks are not permitted.
Operating system | Directory the Java virtual machine will look in for the classes |
---|---|
Linux AIX Solaris HP-UX |
'.../myPacks/UserProcs/' |
Windows | '...\myPacks\UserProcs\' |
The string specified represents the .NET assembly (library or executable), the class within that assembly, and the method within the class that the database manager invokes to execute the procedure being created. The module, class, and method do not need to exist when the CREATE PROCEDURE statement is executed. However, when the procedure is called, the module, class, and method must exist and be accessible from the database server machine, otherwise an error is returned (SQLSTATE 42284).
C++ routines that are compiled with the '/clr' compiler option to indicate that they include managed code extensions must be cataloged as 'LANGUAGE CLR' and not 'LANGUAGE C'. The database manager needs to know that the .NET infrastructure is being utilized in a procedure in order to make necessary runtime decisions. All procedures using the .NET infrastructure must be cataloged as 'LANGUAGE CLR'.
>>-'--assembly--:--class_id--!--method_id--'-------------------><
The name must be enclosed by single quotation marks. Extraneous blanks are not permitted.
The string specified is the OLE programmatic identifier (progid) or class identifier (clsid), and method identifier (method_id), which the database manager invokes to execute the procedure being created by the statement. The programmatic identifier or class identifier, and the method identifier do not need to exist when the CREATE PROCEDURE statement is executed. However, when the procedure is used in the CALL statement, the method identifier must exist and be accessible from the database server machine, otherwise an error results (SQLSTATE 42724).
>>-'--+-progid-+--!--method_id--'------------------------------>< '-clsid--'
The name must be enclosed by single quotation marks. Extraneous blanks are not permitted.
<program_name>.<component_name>.<version>
Because this is only a convention, and not a rule, progids may in fact have a different format.
{nnnnnnnn-nnnn-nnnn-nnnn-nnnnnnnnnnnn}
where
'n' is an alphanumeric character. A clsid is
not interpreted by the database manager, but only forwarded to the
OLE APIs at run time.If a procedure is registered as FENCED, the database manager protects its internal resources (for example, data buffers) from access by the procedure. All procedures have the option of running as FENCED or NOT FENCED. In general, a procedure running as FENCED will not perform as well as a similar one running as NOT FENCED.
Either SYSADM authority, DBADM authority, or a special authority (CREATE_NOT_FENCED) is required to register a procedure as NOT FENCED. Only FENCED can be specified for a procedure with LANGUAGE OLE or NOT THREADSAFE.
LANGUAGE CLR procedures cannot be created when specifying the NOT FENCED clause (SQLSTATE 42601).
For FENCED procedures, THREADSAFE is the default if the LANGUAGE is JAVA or CLR. For all other languages, NOT THREADSAFE is the default. If the procedure is defined with LANGUAGE OLE, THREADSAFE may not be specified (SQLSTATE 42613).
For NOT FENCED procedures, THREADSAFE is the default. NOT THREADSAFE cannot be specified (SQLSTATE 42613).
The commit operation includes the work that is performed by the calling application process and the procedure.
If the procedure returns result sets, the cursors that are associated with the result sets must have been defined as WITH HOLD to be usable after the commit.
No changes to the special registers are passed back to the caller of the procedure.
Non-updatable special registers, such as the datetime special registers, reflect a property of the statement currently executing, and are therefore set to their default values.
This can only be specified when LANGUAGE C, COBOL, CLR, or OLE is used.
Null indicators are not directly passed to the program.
PARAMETER STYLE JAVA procedures do not support the DBINFO or PROGRAM TYPE clauses.
This can only be specified when LANGUAGE C, COBOL, CLR, or OLE is used.
If the database is not a Unicode database, and a procedure with PARAMETER CCSID UNICODE is created, the procedure cannot have any graphic types, the XML type, or user-defined types (SQLSTATE 560C1).
If the database is not a Unicode database, and the alternate collating sequence has been specified in the database configuration, procedures can be created with either PARAMETER CCSID ASCII or PARAMETER CCSID UNICODE. All data passed into and out of the procedure will be converted to the appropriate code page.
This clause cannot be specified with LANGUAGE OLE, LANGUAGE JAVA, or LANGUAGE CLR (SQLSTATE 42613).
If you change session authorization ID (the SESSION_USER special register) using the SET SESSION AUTHORIZATION statement, the Application Authorization ID still returns the value of the SYSTEM_USER special register.
CREATE PROCEDURE PARTS_ON_HAND (IN PARTNUM INTEGER,
OUT COST DECIMAL(7,2),
OUT QUANTITY INTEGER)
EXTERNAL NAME 'parts.onhand'
LANGUAGE JAVA PARAMETER STYLE JAVA
CREATE PROCEDURE ASSEMBLY_PARTS (IN ASSEMBLY_NUM INTEGER,
OUT NUM_PARTS INTEGER,
OUT COST DOUBLE)
EXTERNAL NAME 'parts!assembly'
DYNAMIC RESULT SETS 1 NOT FENCED
LANGUAGE C PARAMETER STYLE GENERAL