CREATE PROCEDURE (SQL)

The CREATE PROCEDURE (SQL) statement creates an SQL procedure at the current server.

Invocation

This statement can be embedded in an application program or issued interactively. It is an executable statement that can be dynamically prepared.

Authorization

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:

  • For the SYSPROCS catalog view and SYSPARMS catalog table:
    • The INSERT privilege on the table, and
    • The system authority *EXECUTE on library QSYS2
  • Administrative authority

The privileges held by the authorization ID of the statement must include at least one of the following:

  • The following system authorities:
    • *USE on the Create Program (CRTPGM) command, and
  • Administrative authority

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:

  • The system authority *ADD to the user profile with that name
  • Administrative authority

Start of changeIf a distinct type or array type is referenced, the privileges held by the authorization ID of the statement must include at least one of the following: End of change

  • Start of changeFor each distinct type or array type identified in the statement:
    • Start of changeThe USAGE privilege on the type, andEnd of change
    • Start of changeThe system authority *EXECUTE on the library containing the distinct type or array typeEnd of change
    End of change
  • Administrative authority

Start of changeTo replace an existing procedure, the privileges held by the authorization ID of the statement must include at least one of the following: End of change

Start of change
  • The following system authorities:
    • The system authority of *OBJMGT on the program or service program object associated with the procedure
    • All authorities needed to DROP the procedure
    • Start of changeThe system authority *READ to the SYSPROCS catalog view and SYSPARMS catalog tableEnd of change
  • Administrative authority
End of change

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.

Syntax

Read syntax diagramSkip visual syntax diagram
>>-CREATE--+------------+--PROCEDURE--procedure-name------------>
           '-OR REPLACE-'                              

>--+-----------------------------------------+------------------>
   '-(--+-------------------------------+--)-'   
        | .-,-------------------------. |        
        | V                           | |        
        '-----parameter-declaration---+-'        

>--+-procedure-definition---------------+----------------------><
   '-WRAPPED--obfuscated-statement-text-'   

procedure-definition

|--option-list--+----------------------+--SQL-routine-body------|
                '-SET OPTION-statement-'                     

Read syntax diagramSkip visual syntax diagram
parameter-declaration

   .-IN----.                                                  
|--+-------+--parameter-name--data-type--+----------------+-----|
   +-OUT---+                             '-default-clause-'   
   '-INOUT-'                                                  

data-type

|--+-built-in-type------+---------------------------------------|
   +-distinct-type-name-+   
   '-array-type-name----'   

default-clause

|--DEFAULT--+-NULL-------------+--------------------------------|
            +-constant---------+   
            +-special-register-+   
            +-global-variable--+   
            '-(--expression--)-'   

Read syntax diagramSkip visual syntax diagram
option-list

   .-LANGUAGE SQL-. (1)  .-NOT DETERMINISTIC-.   
|--+--------------+------+-------------------+------------------>
                         '-DETERMINISTIC-----'   

   .-MODIFIES SQL DATA-.  .-CALLED ON NULL INPUT .   
>--+-------------------+--+----------------------+-------------->
   +-READS SQL DATA----+                             
   '-CONTAINS SQL------'                             

   .-INHERIT SPECIAL REGISTERS-.   
>--+---------------------------+-------------------------------->

   .-DYNAMIC RESULT SETS--0-------.   
>--+------------------------------+----------------------------->
   '-DYNAMIC RESULT SETS--integer-'   

>--+-------------------------+--+---------------------+--------->
   '-SPECIFIC--specific-name-'  +-DISALLOW DEBUG MODE-+   
                                +-ALLOW DEBUG MODE----+   
                                '-DISABLE DEBUG MODE--'   

   .-FENCED-----.  .-PROGRAM TYPE MAIN-.   
>--+------------+--+-------------------+------------------------>
   '-NOT FENCED-'  '-PROGRAM TYPE SUB--'   

   .-OLD SAVEPOINT LEVEL-.  .-COMMIT ON RETURN NO--.   
>--+---------------------+--+----------------------+------------>
   '-NEW SAVEPOINT LEVEL-'  '-COMMIT ON RETURN YES-'   

>--+---------------------------------------------------------------+--|
   |                               .-DEFAULT---------------------. |   
   '-CONCURRENT ACCESS RESOLUTION--+-+-USE CURRENTLY COMMITTED-+-+-'   
                                   | '-U-----------------------' |     
                                   '-+-WAIT FOR OUTCOME-+--------'     
                                     '-W----------------'              

Notes:
  1. The optional clauses can be specified in a different order.
Read syntax diagramSkip visual syntax diagram
SQL-routine-body

|--+-SQL-control-statement--------------------------+-----------|
   +-ALLOCATE CURSOR-statement----------------------+   
   +-ALLOCATE DESCRIPTOR-statement------------------+   
   +-ALTER FUNCTION-statement-----------------------+   
   +-ALTER PROCEDURE-statement----------------------+   
   +-ALTER SEQUENCE-statement-----------------------+   
   +-ALTER TABLE-statement--------------------------+   
   +-ASSOCIATE LOCATORS-statement-------------------+   
   +-COMMENT-statement------------------------------+   
   +-COMMIT-statement-------------------------------+   
   +-CONNECT-statement------------------------------+   
   +-CREATE ALIAS-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 TYPE-statement--------------------------+   
   +-CREATE VIEW-statement--------------------------+   
   +-DEALLOCATE DESCRIPTOR-statement----------------+   
   +-DECLARE GLOBAL TEMPORARY TABLE-statement-------+   
   +-DELETE-statement-------------------------------+   
   +-DESCRIBE-statement-----------------------------+   
   +-DESCRIBE CURSOR-statement----------------------+   
   +-DESCRIBE INPUT-statement-----------------------+   
   +-DESCRIBE PROCEDURE-statement-------------------+   
   +-DESCRIBE TABLE-statement-----------------------+   
   +-DISCONNECT-statement---------------------------+   
   +-DROP-statement---------------------------------+   
   +-EXECUTE IMMEDIATE-statement--------------------+   
   +-GET DESCRIPTOR-statement-----------------------+   
   +-GRANT-statement--------------------------------+   
   +-INSERT-statement-------------------------------+   
   +-LABEL-statement--------------------------------+   
   +-LOCK TABLE-statement---------------------------+   
   +-MERGE-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 DECFLOAT ROUNDING MODE-statement---+   
   +-SET CURRENT DEGREE-statement-------------------+   
   +-SET CURRENT IMPLICIT XMLPARSE OPTION-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--------------------------'   

Read syntax diagramSkip visual syntax diagram
built-in-type

|--+-+---SMALLINT---+-------------------------------------------------------------------------------------+--|
   | +-+-INTEGER-+--+                                                                                     |   
   | | '-INT-----'  |                                                                                     |   
   | '---BIGINT-----'                                                                                     |   
   |                  .-(5,0)------------------------.                                                    |   
   +-+-+-DECIMAL-+-+--+------------------------------+----------------------------------------------------+   
   | | '-DEC-----' |  |             .-,0--------.    |                                                    |   
   | '-+-NUMERIC-+-'  '-(--integer--+-----------+--)-'                                                    |   
   |   '-NUM-----'                  '-, integer-'                                                         |   
   |          .-(--53--)------.                                                                           |   
   +-+-FLOAT--+---------------+-+-------------------------------------------------------------------------+   
   | |        '-(--integer--)-' |                                                                         |   
   | +-REAL---------------------+                                                                         |   
   | |         .-PRECISION-.    |                                                                         |   
   | '-DOUBLE--+-----------+----'                                                                         |   
   |             .-(--34--)-.                                                                             |   
   +---DECFLOAT--+----------+-----------------------------------------------------------------------------+   
   |             '-(--16--)-'                                                                             |   
   |                    .-(--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--)-------.                                                        |   
   +-+-+-+-NATIONAL CHARACTER-+--+---------------+----------+---------------------+--+------------------+-+   
   | | | +-NATIONAL CHAR------+  '-(--integer--)-'          |                     |  '-normalize-clause-' |   
   | | | '-NCHAR--------------'                             |                     |                       |   
   | | '-+-+-NATIONAL CHARACTER-+--VARYING-+--(--integer--)-'                     |                       |   
   | |   | +-NATIONAL CHAR------+          |                                      |                       |   
   | |   | '-NCHAR--------------'          |                                      |                       |   
   | |   '-NVARCHAR------------------------'                                      |                       |   
   | |                                                   .-(--1M--)-------------. |                       |   
   | '-----+-+-NATIONAL CHARACTER-+--LARGE OBJECT-+------+----------------------+-'                       |   
   |       | '-NCHAR--------------'               |      '-(--integer--+---+--)-'                         |   
   |       '-NCLOB--------------------------------'                    +-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----------------------------------------------------------------------------------------------+   
   '---XML------------------------------------------------------------------------------------------------'   

ccsid-clause

|--CCSID--integer--+------------------+-------------------------|
                   '-normalize-clause-'   

normalize-clause

   .-NOT NORMALIZED-.   
|--+-NORMALIZED-----+-------------------------------------------|

Description

Start of changeOR REPLACEEnd of change
Start of changeSpecifies to replace the definition for the procedure if one exists at the current server. The existing definition is effectively dropped before the new definition is replaced in the catalog with the exception that privileges that were granted on the procedure are not affected. This option is ignored if a definition for the procedure does not exist at the current server. To replace an existing procedure, the specific-name and procedure-name of the new definition must be the same as the specific-name and procedure-name of the old definition, or the signature of the new definition must match the signature of the old definition. Otherwise, a new procedure is created.End of change
procedure-name
Names the procedure. The combination of name, schema name, the number of parameters must not identify a procedure that exists at the current server.

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:

  • If the value of the CURRENT SCHEMA special register is *LIBL, the procedure will be created in the current library (*CURLIB).
  • Otherwise, the procedure will be created in the current schema.

The schema-name cannot be QSYS2, QSYS, QTEMP, or SYSIBM.

(parameter-declaration,...)
Specifies the number of parameters of the procedure and the data type of each parameter. A parameter for a procedure can be used only for input, only for output, or for both input and output.

The maximum number of parameters allowed in an SQL procedure is 1024.

IN
Identifies the parameter as an input parameter to the procedure. Any changes made to the parameter within the procedure are not available to the calling SQL application when control is returned. The default is IN.
OUT
Identifies the parameter as an output parameter that is returned by the procedure. If the parameter is not set within the procedure, the null value is returned.
INOUT
Identifies the parameter as both an input and output parameter for the procedure. If the parameter is not set within the procedure, its input value is returned. Start of changeIf an INOUT parameter is defined with a default and the default is used when calling the procedure, no value for the parameter is returned.End of change
parameter-name
Names the parameter. The name cannot be the same as any other parameter-name for the procedure.
data-type
Specifies the data type of the parameter. The data type can be a built-in data type or a distinct data type.
built-in-type
Specifies a built-in data type. For a more complete description of each built-in data type, see CREATE TABLE.
distinct-type-name
Specifies a distinct type. The length, precision, or scale attributes for the parameter are those of the source type of the distinct type (those specified on CREATE TYPE). For more information on creating a distinct type, see CREATE TYPE (Distinct). .

If the name of the distinct type is unqualified, the database manager resolves the schema name by searching the schemas in the SQL path.

Start of changearray-type-nameEnd of change
Start of changeSpecifies an array type.

If the name of the array type is unqualified, the database manager resolves the schema name by searching the schemas in the SQL path.

End of change

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.

Start of changedefaultEnd of change
Start of changeSpecifies a default value for the parameter. The default can be a constant, a special register, a global variable, an expression, or the keyword NULL. The expression is any expression defined in Expressions, that does not include an aggregate function or column name. If a default value is not specified, the parameter has no default and cannot be omitted on invocation of the procedure. The maximum length of the expression string is 64K.

The default expression must not modify SQL data. The expression must be assignment compatible to the parameter data type. All objects referenced in a default expression must exist when the procedure is created.

Any comma in the default expression that is intended as a separator of numeric constants in a list must be followed by a space.

A default cannot be specified:
  • for an OUT parameter.
  • for a parameter of type array.
End of change
LANGUAGE SQL
Specifies that this procedure is written exclusively in SQL.
SPECIFIC specific-name
Specifies a unique name for the function. For more information on specific names, see Specifying a specific name for a procedure.
DETERMINISTIC or NOT DETERMINISTIC
Specifies whether the procedure returns the same results each time the procedure is called with the same IN and INOUT arguments. The default is NOT DETERMINISTIC.
NOT DETERMINISTIC
The procedure may not return the same result each time the procedure is called with the same IN and INOUT arguments, even when the referenced data in the database has not changed.
DETERMINISTIC
The procedure always returns the same results each time the procedure is called with the same IN and INOUT arguments, provided the referenced data in the database has not changed.
MODIFIES SQL DATA, READS SQL DATA, or CONTAINS SQL
Specifies the classification of SQL statements that this procedure, or any routine called by this procedure, can execute. The database manager verifies that the SQL statements issued by the procedure and all routines called by the procedure are consistent with this specification. The default is MODIFIES SQL DATA. Start of changeThis option is used for parameter default expressions.End of change For the classification of each statement, see Characteristics of SQL statements.
MODIFIES SQL DATA
Specifies that the procedure can execute any SQL statement except statements that are not supported in procedures.
READS SQL DATA
Specifies that the procedure can execute statements with a data access classification of READS SQL DATA, CONTAINS SQL, or NO SQL.
CONTAINS SQL
Specifies that the procedure can only execute statements with a data access classification of CONTAINS SQL or NO SQL.
CALLED ON NULL INPUT
Specifies that the procedure is to be called if any or all argument values are null. This specification means that the procedure must be coded to test for null argument values.
INHERIT SPECIAL REGISTERS
Specifies that existing values of special registers are inherited upon entry to the procedure.
DYNAMIC RESULT SETS integer
Specifies the maximum number of result sets that can be returned from the procedure. The minimum value for integer is zero and the maximum value is 32768. The default is DYNAMIC RESULT SETS 0.

Result sets are returned in the order in which the corresponding cursors are opened, unless a SET RESULT SETS statement is executed in the procedure. If the number of cursors still open for result sets when the procedure ends exceeds the maximum number specified on the DYNAMIC RESULT SETS clause, a warning is returned on the CALL statement and the number of result sets specified on the DYNAMIC RESULT SETS clause is returned.

If the SET RESULT SETS statement is issued, the number of results returned is the minimum of the number of result sets specified on this keyword and the SET RESULT SETS statement. If the SET RESULT SETS statement specifies a number larger than the maximum number of result sets, a warning is returned. Note that any result sets from cursors that have a RETURN TO CLIENT attribute are included in the number of result sets of the outermost procedure.

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.

Start of changeFor more information about result sets, see SET RESULT SETS.End of change

DISALLOW DEBUG MODE, ALLOW DEBUG MODE, or DISABLE DEBUG MODE
Indicates whether the procedure is created so it can be debugged by the Unified Debugger. If DEBUG MODE is specified, a DBGVIEW option in the SET OPTION statement must not be specified.
DISALLOW DEBUG MODE
The procedure cannot be debugged by the Unified Debugger. When the DEBUG MODE attribute of the procedure is DISALLOW, the procedure can be subsequently altered to change the debug mode attribute.
ALLOW DEBUG MODE
The procedure can be debugged by the Unified Debugger. When the DEBUG MODE attribute of the procedure is ALLOW, the procedure can be subsequently altered to change the debug mode attribute.
DISABLE DEBUG MODE
The procedure cannot be debugged by the Unified Debugger. When the DEBUG MODE attribute of the procedure is DISABLE, the procedure cannot be subsequently altered to change the debug mode attribute.

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.

FENCED or NOT FENCED
This parameter is allowed for compatibility with other products and is not used by DB2® for i.
PROGRAM TYPE MAIN or PROGRAM TYPE SUB
Specifies whether the procedure is created as a program (*PGM) or a procedure in a service program (*SRVPGM).
PROGRAM TYPE MAIN
Specifies that the procedure is created as a program (*PGM).
PROGRAM TYPE SUB
Specifies that the procedure is created as a procedure in a service program (*SRVPGM).

Start of changePROGRAM TYPE SUB procedures usually perform slightly better than PROGRAM TYPE MAIN procedures. If the number of parameters exceeds 400, PROGRAM TYPE MAIN will be used instead of PROGRAM TYPE SUB.End of change

OLD SAVEPOINT LEVEL or NEW SAVEPOINT LEVEL
Specifies whether a new savepoint level is to be created on entry to the procedure.
OLD SAVEPOINT LEVEL
A new savepoint level is not created. Any SAVEPOINT statements issued within the procedure with OLD SAVEPOINT LEVEL implicitly or explicitly specified on the SAVEPOINT statement are created at the same savepoint level as the caller of the procedure. This is the default.
NEW SAVEPOINT LEVEL
A new savepoint level is created on entry to the procedure. Any savepoints set within the procedure are created at a savepoint level that is nested deeper than the level at which this procedure was invoked. Therefore, the name of any new savepoint set within the procedure will not conflict with any existing savepoints set in higher savepoint levels (such as the savepoint level of the calling program) with the same name.
COMMIT ON RETURN
Specifies whether the database manager commits the transaction immediately on return from the procedure.
NO
The database manager does not issue a commit when the procedure returns. NO is the default.
YES
The database manager issues a commit if the procedure returns successfully. If the procedure returns with an error, a commit is not issued.

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.

Start of changeCONCURRENT ACCESS RESOLUTIONEnd of change
Start of changeSpecifies whether the database manager should wait for data that is in the process of being updated. DEFAULT is the default.
DEFAULT
Specifies that the concurrent access resolution is not explicitly set for this procedure. The value that is in effect when the procedure is called will be used.
WAIT FOR OUTCOME
Specifies that the database manager is to wait for the commit or rollback of data in the process of being updated.
USE CURRENTLY COMMITTED
Specifies that the database manager is to use the currently committed version of the data when encountering data that is in the process of being updated.
When the lock contention is between a read transaction and a delete or update transaction, the clause is applicable to scans with isolation level CS (but not for CS KEEP LOCKS).
End of change
Start of changeWRAPPED obfuscated-statement-textEnd of change
Start of changeSpecifies the encoded definition of the procedure. A CREATE PROCEDURE statement can be encoded using the WRAP scalar function.End of change
SET OPTION-statement
Specifies the options that will be used to create the procedure. Start of changeThese options also apply to any default value expressions.End of change For example, to create a debuggable procedure, the following statement could be included:
SET OPTION DBGVIEW = *SOURCE 
Start of changeThe default values for the options depend the options in effect at create time.End of change For more information, see SET OPTION.

The options Start of changeCNULIGN,End of change CNULRQD, COMPILEOPT, NAMING, and SQLCA are not allowed in the CREATE PROCEDURE statement. Start of changeThe following options are used when processing default value expressions: ALWCPYDTA, CONACC, DATFMT, DATSEP, DECFLTRND, DECMPT, DECRESULT, DFTRDBCOL, LANGID, SQLCURRULE, SQLPATH, SRTSEQ, TGTRLS, TIMFMT, and TIMSEP.End of change

SQL-routine-body
Specifies a single Start of changeSQL-procedure-statementEnd of change, including a compound statement. See SQL control statements for more information about defining SQL procedures.

Start of changeThe SET TRANSACTION statement is 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.End of change

ALTER PROCEDURE (SQL), ALTER FUNCTION (SQL Scalar), and ALTER FUNCTION (SQL Table) with a REPLACE keyword are not allowed in an SQL-routine-body.

Notes

General considerations for defining procedures: See CREATE PROCEDURE for general information on defining procedures.

Procedure ownership: If SQL names were specified:

  • If a user profile with the same name as the schema into which the procedure is created exists, the owner of the procedure is that user profile.
  • Otherwise, the owner of the procedure is the user profile or group user profile of the job executing the statement.

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.

Start of change
REPLACE rules: When a procedure is recreated by REPLACE:
  • Any existing comment or label is discarded.
  • Authorized users are maintained. The object owner could change.
  • Current journal auditing is preserved.
End of change

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. Values of arguments passed to a procedure that correspond to OUT parameters are undefined and those that correspond to INOUT parameters are unchanged when an error is returned by the procedure.

Start of changeCreating 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 or service program object is then created using the CRTPGM or CRTSRVPGM 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).End of change

Start of changeWhen an SQL procedure is created, the procedure's attributes are stored in the created program or service program object. If the *PGM or *SRVPGM object is saved and then restored to this or another system, the attributes are used to update the catalogs.End of change

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.

Start of change

Obfuscated statements: A CREATE PROCEDURE statement can be executed in obfuscated form. In an obfuscated statement, only the procedure name and parameters are readable followed by the WRAPPED keyword. The rest of the statement is encoded in such a way that it is not readable but can be decoded by a database server that supports obfuscated statements. Obfuscated statements can be produced by invoking the WRAP scalar function. Any debug options that are specified when the procedure is created from an obfuscated statement are ignored. A procedure that is created from an obfuscated statement cannot be restored to a release where obfuscation is not supported.

End of change
Start of change

Setting of the default value: Parameters of a procedure that are defined with a default value are set to their default value when the procedure is invoked, but only if a value is not supplied for the corresponding argument, or the argument is specified as DEFAULT.

End of change

Dependent objects: An SQL routine is dependent on objects that are referenced in the SQL-routine-body. The names of the dependent objects are stored in catalog view SYSROUTINEDEP. If the object reference in the SQL-routine-body is a fully qualified name or, in SQL naming, if an unqualified name is qualified by the current schema, then the schema name of the object in SYSROUTINEDEP will be set to the specified name or the value of the current schema. Otherwise, the schema name is not set to a specific schema name. If a name is not set to a specific schema name, then DROP and ALTER statements will not be able to determine whether the routine is dependent on the object being altered or dropped.

Syntax alternatives: The following keywords are synonyms supported for compatibility to prior releases. These keywords are non-standard and should not be used:

  • The keywords VARIANT and NOT VARIANT can be used as synonyms for NOT DETERMINISTIC and DETERMINISTIC.
  • The keywords NULL CALL can be used as synonyms for CALLED ON NULL INPUT.
  • DYNAMIC RESULT SET, RESULT SETS, and RESULT SET may be used as synonyms for DYNAMIC RESULT SETS.

Example

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