The CREATE PROCEDURE (SQL) statement creates an SQL procedure at the current server.
This statement can be embedded in an application program or issued interactively. It is an executable statement that can be dynamically prepared.
The privileges held by the authorization ID of the statement must include at least one of the following:
The privileges held by the authorization ID of the statement must include at least one of the following:
The privileges held by the authorization ID of the statement must include at least one of the following:
If SQL names are specified and a user profile exists that has the same name as the library into which the procedure is created, and that name is different from the authorization ID of the statement, then the privileges held by the authorization ID of the statement must include at least one of the following:
If a distinct type is referenced, the privileges held by the authorization ID of the statement must include at least one of the following:
For information on the system authorities corresponding to SQL privileges, see Corresponding System Authorities When Checking Privileges to a Function or Procedure and Corresponding System Authorities When Checking Privileges to a Distinct Type.
>>-CREATE PROCEDURE--procedure-name-----------------------------> >--+-----------------------------------------+------------------> '-(--+-------------------------------+--)-' | .-,-------------------------. | | V | | '-----parameter-declaration---+-' >--LANGUAGE SQL--option-list--+----------------------+--SQL-routine-body->< '-SET OPTION-statement-'
parameter-declaration: .-IN----. |--+-------+--parameter-name--data-type-------------------------| +-OUT---+ '-INOUT-' data-type: |--+-built-in-type------+---------------------------------------| '-distinct-type-name-'
option-list: .-NOT DETERMINISTIC-. (1) .-MODIFIES SQL DATA-. |--+-------------------+-------+-------------------+------------> '-DETERMINISTIC-----' +-READS SQL DATA----+ '-CONTAINS SQL------' .-CALLED ON NULL INPUT-. .-DYNAMIC RESULT SETS--0-------. >--+----------------------+--+------------------------------+---> '-DYNAMIC RESULT SETS--integer-' >--+-------------------------+--+---------------------+---------> '-SPECIFIC--specific-name-' +-DISALLOW DEBUG MODE-+ +-ALLOW DEBUG MODE----+ '-DISABLE DEBUG MODE--' .-FENCED-----. .-OLD SAVEPOINT LEVEL-. >--+------------+--+---------------------+----------------------> '-NOT FENCED-' '-NEW SAVEPOINT LEVEL-' .-COMMIT ON RETURN NO--. >--+----------------------+-------------------------------------| '-COMMIT ON RETURN YES-'
SQL-routine-body: |--+-SQL-control-statement-----------------------+--------------| +-ALLOCATE DESCRIPTOR-statement---------------+ +-ALTER PROCEDURE (External)-statement--------+ +-ALTER SEQUENCE-statement--------------------+ +-ALTER TABLE-statement-----------------------+ +-COMMENT-statement---------------------------+ +-COMMIT-statement----------------------------+ +-CONNECT-statement---------------------------+ +-CREATE ALIAS-statement----------------------+ +-CREATE DISTINCT TYPE-statement--------------+ +-CREATE FUNCTION (External Scalar)-statement-+ +-CREATE FUNCTION (External Table)-statement--+ +-CREATE FUNCTION (Sourced)-statement---------+ +-CREATE INDEX-statement----------------------+ +-CREATE PROCEDURE (External)-statement-------+ +-CREATE SCHEMA-statement---------------------+ +-CREATE SEQUENCE-statement-------------------+ +-CREATE TABLE-statement----------------------+ +-CREATE VIEW-statement-----------------------+ +-DEALLOCATE DESCRIPTOR-statement-------------+ +-DECLARE GLOBAL TEMPORARY TABLE-statement----+ +-DELETE-statement----------------------------+ +-DESCRIBE-statement--------------------------+ +-DESCRIBE INPUT-statement--------------------+ +-DESCRIBE TABLE-statement--------------------+ +-DISCONNECT-statement------------------------+ +-DROP-statement------------------------------+ +-EXECUTE IMMEDIATE-statement-----------------+ +-GET DESCRIPTOR-statement--------------------+ +-GRANT-statement-----------------------------+ +-INSERT-statement----------------------------+ +-LABEL-statement-----------------------------+ +-LOCK TABLE-statement------------------------+ +-REFRESH TABLE-statement---------------------+ +-RELEASE-statement---------------------------+ +-RELEASE SAVEPOINT-statement-----------------+ +-RENAME-statement----------------------------+ +-REVOKE-statement----------------------------+ +-ROLLBACK-statement--------------------------+ +-SAVEPOINT-statement-------------------------+ +-SELECT INTO-statement-----------------------+ +-SET CONNECTION-statement--------------------+ +-SET CURRENT DEBUG MODE-statement------------+ +-SET CURRENT DEGREE-statement----------------+ +-SET DESCRIPTOR-statement--------------------+ +-SET ENCRYPTION PASSWORD-statement-----------+ +-SET PATH-statement--------------------------+ +-SET RESULT SETS-statement-------------------+ +-SET SCHEMA-statement------------------------+ +-SET TRANSACTION-statement-------------------+ +-UPDATE-statement----------------------------+ '-VALUES INTO-statement-----------------------'
built-in-type: |--+-+---SMALLINT---+--------------------------------------------------------------------------+--| | +-+-INTEGER-+--+ | | | '-INT-----' | | | '---BIGINT-----' | | .-(5,0)------------------------. | +-+-+-DECIMAL-+-+--+------------------------------+-----------------------------------------+ | | '-DEC-----' | | .-,0--------. | | | '-NUMERIC-----' '-(--integer--+-----------+--)-' | | '-, integer-' | | .-(--53--)------. | +-+-FLOAT--+---------------+-+--------------------------------------------------------------+ | | '-(--integer--)-' | | | +-REAL---------------------+ | | | .-PRECISION-. | | | '-DOUBLE--+-----------+----' | | .-(--1--)-------. | +-+-+-+-CHARACTER-+--+---------------+----------+--+----------------+---------------------+-+ | | | '-CHAR------' '-(--integer--)-' | +-FOR BIT DATA---+ | | | | '-+-+-CHARACTER-+--VARYING-+--(--integer--)-' +-FOR SBCS DATA--+ | | | | | '-CHAR------' | +-FOR MIXED DATA-+ | | | | '-VARCHAR----------------' '-ccsid-clause---' | | | | .-(--1M--)-------------. | | | '-----+-+-CHARACTER-+--LARGE OBJECT-+------+----------------------+--+----------------+-' | | | '-CHAR------' | '-(--integer--+---+--)-' +-FOR SBCS DATA--+ | | '-CLOB------------------------' +-K-+ +-FOR MIXED DATA-+ | | +-M-+ '-ccsid-clause---' | | '-G-' | | .-(--1--)-------. | +-+---GRAPHIC----+---------------+-------+--+--------------+--------------------------------+ | | '-(--integer--)-' | '-ccsid-clause-' | | +-+-GRAPHIC VARYING-+--(--integer--)---+ | | | '-VARGRAPHIC------' | | | | .-(--1M--)-------------. | | | '---DBCLOB----+----------------------+-' | | '-(--integer--+---+--)-' | | +-K-+ | | +-M-+ | | '-G-' | | .-(--1--)-------. | +-+-+-BINARY--+---------------+---------+-----------------+---------------------------------+ | | | '-(--integer--)-' | | | | | '-+-BINARY VARYING-+--(--integer--)-' | | | | '-VARBINARY------' | | | | .-(--1M--)-------------. | | | '---+-BLOB----------------+----+----------------------+-' | | '-BINARY LARGE OBJECT-' '-(--integer--+---+--)-' | | +-K-+ | | +-M-+ | | '-G-' | +-+-DATE-------------------+----------------------------------------------------------------+ | | .-(--0--)-. | | | +-TIME--+---------+------+ | | | .-(--6--)-. | | | '-TIMESTAMP--+---------+-' | | .-(--200--)-----. | +---DATALINK--+---------------+--+--------------+-------------------------------------------+ | '-(--integer--)-' '-ccsid-clause-' | '---ROWID-----------------------------------------------------------------------------------' ccsid-clause: .-NOT NORMALIZED-. |--CCSID--integer--+----------------+---------------------------| '-NORMALIZED-----'
For SQL naming, the procedure will be created in the schema specified by the implicit or explicit qualifier.
For system naming, the procedure will be created in the schema specified by the qualifier. If no qualifier is specified:
The maximum number of parameters allowed in an SQL procedure is 1024.
If the name of the distinct type is unqualified, the database manager resolves the schema name by searching the schemas in the SQL path.
If a CCSID is specified, the parameter will be converted to that CCSID prior to passing it to the procedure. If a CCSID is not specified, the CCSID is determined by the default CCSID at the current server at the time the procedure is called.
The result sets are scrollable if the cursor is used to return a result set and the cursor is scrollable. If a cursor is used to return a result set, the result set starts with the current position. Thus, if 5 FETCH NEXT operations have been performed prior to returning from the procedure, the result set will start with the 6th row of the result set.
Result sets are only returned if the procedure is directly called or if the procedure is a RETURN TO CLIENT procedure and is indirectly called from ODBC, JDBC, OLE DB, .NET, the SQL Call Level Interface, or the iSeries Access Family Optimized SQL API. For more information about result sets, see SET RESULT SETS.
If specific-name is not specified, it is the same as the procedure name. If a function or procedure with that specific name already exists, a unique name is generated similar to the rules used to generate unique table names.
If DEBUG MODE is not specified, but a DBGVIEW option in the SET OPTION statement is specified, the procedure cannot be debugged by the Unified Debugger, but may be debugged by the system debug facilities. If neither DEBUG MODE nor a DBGVIEW option is specified, the debug mode used is from the CURRENT DEBUG MODE special register.
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.
SET OPTION DBGVIEW = *SOURCE
For
more information, see SET OPTION.
The options CLOSQLCSR, CNULRQD, COMPILEOPT, NAMING, and SQLCA are not allowed in the CREATE PROCEDURE statement.
CONNECT, SET CONNECTION, RELEASE, DISCONNECT, and SET TRANSACTION statements are not allowed in a procedure that is running on a remote application server. COMMIT and ROLLBACK statements are not allowed in an ATOMIC SQL procedure or in a procedure that is running on a connection to a remote application server.
General considerations for defining procedures: See CREATE PROCEDURE for general information on defining procedures.
Procedure ownership: If SQL names were specified:
If system names were specified, the owner of the procedure is the user profile or group user profile of the job executing the statement.
Procedure authority: If SQL names are used, procedures are created with the system authority of *EXCLUDE on *PUBLIC. If system names are used, procedures are created with the authority to *PUBLIC as determined by the create authority (CRTAUT) parameter of the schema.
If the owner of the procedure is a member of a group profile (GRPPRF keyword) and group authority is specified (GRPAUT keyword), that group profile will also have authority to the procedure.
Error handling in procedures: Consideration should be given to possible exceptions that can occur for each SQL statement in the body of a procedure. Any exception SQLSTATE that is not handled within the procedure using a handler within a compound statement, results in the exception SQLSTATE being returned to the caller of the procedure.
Creating the procedure: When an SQL procedure is created, SQL creates a temporary source file that will contain C source code with embedded SQL statements. A program object is then created using the CRTPGM command. The SQL options used to create the program are the options that are in effect at the time the CREATE PROCEDURE statement is executed. The program is created with ACTGRP(*CALLER).
When an SQL procedure is created, the procedure's attributes are stored in the created program object. If the *PGM object is saved and then restored to this or another system, the catalogs are automatically updated with those attributes.
During restore of the procedure:
The specific procedure name is used as the name of the member in the source file and the name of the program object, if it is a valid system name. If the procedure name is not a valid system name, a unique name is generated. If a source file member with the same name already exists, the member is overlaid. If a module or a program with the same name already exists, the objects are not overlaid, and a unique name is generated. The unique names are generated according to the rules for generating system table names.
Invoking the procedure: If a DECLARE PROCEDURE statement defines a procedure with the same name as a created procedure, and a static CALL statement where the procedure name is not identified by a variable is executed from the same source program, the attributes from the DECLARE PROCEDURE statement will be used rather than the attributes from the CREATE PROCEDURE statement.
The CREATE PROCEDURE statement applies to static and dynamic CALL statements as well as to a CALL statement where the procedure name is identified by a variable.
SQL procedures must be called using the SQL CALL statement. When called, the SQL procedure runs in the activation group of the calling program.
Syntax alternatives: The following keywords are synonyms supported for compatibility to prior releases. These keywords are non-standard and should not be used:
Create an SQL procedure that returns the median staff salary. Return a result set containing the name, position, and salary of all employees who earn more than the median salary.
CREATE PROCEDURE MEDIAN_RESULT_SET (OUT medianSalary DECIMAL(7,2)) LANGUAGE SQL DYNAMIC RESULT SETS 1 BEGIN DECLARE v_numRecords INTEGER DEFAULT 1; DECLARE v_counter INTEGER DEFAULT 0; DECLARE c1 CURSOR FOR SELECT salary FROM staff ORDER BY salary; DECLARE c2 CURSOR WITH RETURN FOR SELECT name, job, salary FROM staff WHERE salary > medianSalary ORDER BY salary; DECLARE EXIT HANDLER FOR NOT FOUND SET medianSalary = 6666; SET medianSalary = 0; SELECT COUNT(*) INTO v_numRecords FROM STAFF; OPEN c1; WHILE v_counter < (v_numRecords / 2 + 1) DO FETCH c1 INTO medianSalary; SET v_counter = v_counter + 1; END WHILE; CLOSE c1; OPEN c2; END