
CREATE FUNCTION (SQL table)
The CREATE FUNCTION (SQL table) statement creates an SQL table function at the current server. The function returns a set of rows.
Invocation
This statement can only be dynamically
prepared only if DYNAMICRULES run behavior is implicitly or explicitly specified.
Authorization
The privilege set that is defined below must include at least one of the following privileges or authorities:
- The CREATEIN privilege on the schema
- SYSADM authority
- SYSCTRL authority
System DBADM
The authorization ID that matches the schema name implicitly has the CREATEIN privilege on the schema.
If the authorization ID that is used to
create the function has installation SYSADM authority, the function is identified as system-defined
function.
If a distinct type is referenced (i.e. as the data type of a parameter or SQL variable), the privilege set must also include at least one of the following:
- Ownership of the distinct type
- The USAGE privilege on the distinct type
- SYSADM authority
SYSDBADM authority
If the function uses a table as a parameter, the privilege set must also include at least one of the following:
- Ownership of the table
- The SELECT privilege on the table
- SYSADM authority
At least one of the
following additional privileges is required if the SECURED option is specified

- SECADM authority
- CREATE_SECURE_OBJECT privilege

Privilege set: If the statement is embedded in an application program, the privilege set is the set of privileges that are held by the owner of the plan or package. If the owner is a role, matching of the implicit schema name does not apply and the role must include one of the previously listed privileges or authorities.
If the statement is dynamically prepared and is not running in a trusted context for which the ROLE AS OBJECT OWNER clause is specified, the privilege set is the set of privileges that are held by the SQL authorization ID of the process. If the schema name is not the same as the SQL authorization ID of the process, one of the following conditions must be met:
- The privilege set includes SYSADM authority
- The privilege set includes SYSCTRL authority
- The SQL authorization ID of the process has the CREATEIN privilege on the schema
Syntax
>>-CREATE FUNCTION--function-name-------------------------------> >--(--+-------------------------------+--)----------------------> | .-,-------------------------. | | V | | '---| parameter-declaration |-+-' >----| function-definition |-----------------------------------><
parameter-declaration: >>-parameter-name--| parameter-type |--------------------------><
parameter-type: >>-+-| data-type1 |-------------------------+------------------>< '-TABLE LIKE--+-table-name-+--AS LOCATOR-' '-view-name--'
data-type1, data-type2: >>-+-| built-in-type |--+-------------------------------------->< '-distinct-type-name-'
built-in-type: >>-+-+-SMALLINT----+-------------------------------------------------------------------------------------------------+->< | +-+-INTEGER-+-+ | | | '-INT-----' | | | '-BIGINT------' | | .-(5,0)--------------------. | +-+-DECIMAL-+--+--------------------------+-----------------------------------------------------------------------+ | +-DEC-----+ '-(integer-+-----------+-)-' | | '-NUMERIC-' '-, integer-' | | .-(53)------. | +-+-FLOAT--+-----------+--+---------------------------------------------------------------------------------------+ | | '-(integer)-' | | | +-REAL------------------+ | | | .-PRECISION-. | | | '-DOUBLE--+-----------+-' | | .-(34)-. | +-DECFLOAT--+------+----------------------------------------------------------------------------------------------+ | '-(16)-' | | .-(1)-------. | +-+-+-+-CHARACTER-+--+-----------+----------+----+--------------------+--+----------------------+---------------+-+ | | | '-CHAR------' '-(integer)-' | '-CCSID--+-ASCII---+-' '-FOR--+-SBCS--+--DATA-' | | | | '-+-+-CHARACTER-+--VARYING-+--(integer)-' +-EBCDIC--+ +-MIXED-+ | | | | | '-CHAR------' | '-UNICODE-' '-BIT---' | | | | '-VARCHAR----------------' | | | | .-(1M)-------------. | | | '-+-+-CHARACTER-+--LARGE OBJECT-+--+------------------+----+--------------------+--+----------------------+---' | | | '-CHAR------' | '-(integer-+---+-)-' '-CCSID--+-ASCII---+-' '-FOR--+-SBCS--+--DATA-' | | '-CLOB------------------------' +-K-+ +-EBCDIC--+ '-MIXED-' | | +-M-+ '-UNICODE-' | | '-G-' | | .-(1)-------. | +-+-GRAPHIC--+-----------+-------+--+--------------------+--------------------------------------------------------+ | | '-(integer)-' | '-CCSID--+-ASCII---+-' | | +-VARGRAPHIC--(--integer--)----+ +-EBCDIC--+ | | | .-(1M)-------------. | '-UNICODE-' | | '-DBCLOB--+------------------+-' | | '-(integer-+---+-)-' | | +-K-+ | | +-M-+ | | '-G-' | | .-(1)-------. | +-+-BINARY--+-----------+-------------------------+---------------------------------------------------------------+ | | '-(integer)-' | | | +-+-BINARY VARYING-+-(integer)------------------+ | | | '-VARBINARY------' | | | | .-(1M)-------------. | | | '-+-BINARY LARGE OBJECT-+--+------------------+-' | | '-BLOB----------------' '-(integer-+---+-)-' | | +-K-+ | | +-M-+ | | '-G-' | +-+-DATE------------------------------------------------+---------------------------------------------------------+ | +-TIME------------------------------------------------+ | | | .-(--6--)-------. .-WITHOUT TIME ZONE-. | | | '-TIMESTAMP--+---------------+--+-------------------+-' | | '-(--integer--)-' '-WITH TIME ZONE----' | +-ROWID-----------------------------------------------------------------------------------------------------------+ '-XML-------------------------------------------------------------------------------------------------------------'
option-list: (1) .-LANGUAGE SQL-. >>-------+--------------+--+-------------------------+----------> '-SPECIFIC--specific-name-' .-NOT DETERMINISTIC-. .-EXTERNAL ACTION----. >--+-------------------+--+--------------------+----------------> '-DETERMINISTIC-----' '-NO EXTERNAL ACTION-' .-READS SQL DATA-. .-CALLED ON NULL INPUT-. >--+----------------+--+----------------------+-----------------> '-CONTAINS SQL---' .-INHERIT SPECIAL REGISTERS-. .-STATIC DISPATCH-. >--+---------------------------+--+-----------------+-----------> >--+----------------------+--+------------------------------+---> '-CARDINALITY--integer-' '-PARAMETER CCSID--+-ASCII---+-' +-EBCDIC--+ '-UNICODE-' .-NOT SECURED-. >--+-------------+--------------------------------------------->< '-SECURED-----'
- The options in the option-list can be specified in any order. However, the same clause cannot be specified more than one time.
function-definition .-,-----------------------. V | >>-RETURNS TABLE--(----column-name--data-type2-+--)-------------> >--| option-list |--| SQL-routine-body |-----------------------><
SQL-routine-body: >>-+-RETURN statement---------------------+-------------------->< '-BEGIN--ATOMIC--RETURN statement--END-'
Description
- function-name
- Names the user-defined function. The name is implicitly or explicitly qualified by a schema name. The combination of the name, the schema name, the number of parameters, and the data type of each parameter (without regard to any length, precision, scale, subtype, or encoding scheme attribute of the data type) must not identify a user-defined function that exists at the current server.
- (parameter-declaration,...)
- Identifies the number of input parameters of the function, and specifies the name and data type
of each parameter. All of the parameters for a function are input parameters and are nullable. There
must be one entry in the list for each parameter that the function expects to receive.
- parameter-name
- Specifies the name of the input parameter. Each name in the parameter list must not be the same as any other name.
- data-type1
- Specifies the data type of the input parameter. The data type can be a built-in data type or a
distinct type.
- built-in-type
- The data type of the parameter is a built-in data type.
For more information on the data types, including the subtype of character data types (the FOR subtype DATA clause), see built-in types. However, the varying length string data types have different maximum lengths for this statement than for the CREATE TABLE statement. The maximum lengths for parameters (and SQL variables) are as follows:
- VARCHAR or VARBINARY: 32704
- VARGRAPHIC: 16352
For parameters with a character or graphic data type, the PARAMETER CCSID clause or the CCSID clause indicates the encoding scheme of the parameter. If you do not specify either of the CCSID clauses, the encoding scheme is the value of the DEF ENCODING SCHEME field on installation panel DSNTIPF.
Although an input parameter with a character data type has an implicitly or explicitly specified subtype (BIT, SBCS, or MIXED), the value that is actually passed in the input parameter can have any subtype. Therefore, conversion of the input data to the subtype of the parameter might occur when the function is invoked. With ASCII or EBCDIC, an error occurs if mixed data that actually contains DBCS characters is used as the value for an input parameter that is declared with an SBCS subtype.
- distinct-type-name
- The data type of the parameter is a distinct type. Any length, precision, scale, subtype, or encoding scheme attributes for the parameter are those of the source type for the distinct type.
- TABLE LIKE table-name AS LOCATOR
- Specifies that the parameter is a transition table. However, when the function is invoked, the
actual values in the transition table are not passed to the function. A single value is passed
instead. This value is a locator for the table, which the function uses to access the columns of the
transition table. The table that is identified can contain XML columns; however, the function cannot
reference those XML columns.
A function with a table parameter can only be invoked from the triggered action of a trigger.
- RETURNS TABLE
Specifies that the output of the function is a table. The RETURN statement in an SQL table function must return a table result. The parentheses that follow the RETURNS TABLE keyword delimit a list of name and data type pairs of the columns of the output table. All columns of the output table are nullable.
- column-name
- Specifies the name of the column. The name cannot be qualified, and must be unique within the RETURNS TABLE clause for the function.
- data-type2
- Specifies the data type and attributes of the column of the output table.
For SQL table functions, the result table of the function might include multiple encoding schemes – similar to what a view definition can include.
- LANGUAGE SQL
- Specifies that the function is written exclusively in SQL.
- SPECIFIC specific-name
- Specifies a unique name for the function.
- NOT DETERMINISTIC or DETERMINISTIC
- Specifies whether the function returns the same results each time that the function is invoked with the same input arguments. DB2® does not verify that the function program is consistent with the specification of NOT DETERMINISTIC or DETERMINISTIC.
- NOT DETERMINISTIC
- Specifies that the function might not return the same result table each time that the function
is invoked with the same input arguments, even when the referenced data in the database has not
changed. The function depends on some state values that might affect the results.
When the MATERIALIZE_NODET_SQLTUDF subsystem parameter is set to YES, DB2 disables the merging of SQL table functions that are defined with this option.
An example of a table function that is not deterministic is one which references special registers, other functions that are not deterministic, or a sequence in a way that affects the table function's result table. NOT DETERMINISTIC is the default.
- DETERMINISTIC
Specifies that the function always returns the same result table each time that the function is invoked with the same input arguments (provided that the referenced data in the database has not changed). DB2 enables the merging of SQL table functions that are defined with this option.
- EXTERNAL ACTION or NO EXTERNAL ACTION
- Specifies whether the function contains an external action. DB2 does not verify that the function program is consistent with the specification of EXTERNAL ACTION or NO EXTERNAL ACTION.
- EXTERNAL ACTION
- The function performs some external action (outside the scope of the function program). Thus, the function must be invoked with each successive function invocation. EXTERNAL ACTION must be specified if the function invokes another function that has external actions. EXTERNAL ACTION is the default.
- NO EXTERNAL ACTION
- The function does not perform any external action. It need not be called with each successive function invocation. Functions that are defined with NO EXTERNAL ACTION might perform better than functions that are defined with EXTERNAL ACTION because the function might not be invoked for each successive function invocation.
- READS SQL DATA or CONTAINS SQL
- Specifies the classification of SQL statements and nested routines that this routine can execute
or invoke. The database manager verifies that the SQL statements issued by the function, and all
routines locally invoked by the routine, are consistent with this specification; the verification is
not performed when nested remote routines are invoked. For the classification of each statement, see
SQL statement data access classification for routines.
- READS SQL DATA
- Specifies that the function can execute statements with a data access indication of READS SQL
DATA or CONTAINS SQL. The function cannot execute SQL statements that modify data.
READS SQL DATA is the default.
- CONTAINS SQL
- Specifies that the function can execute only SQL statements with a data access indication of CONTAINS SQL. The function cannot execute statements that read or modify data.
- CALLED ON NULL INPUT
- Specifies that the function is called regardless of whether any of the input arguments are null,
making the function responsible for testing for null argument values. The function can return an
empty table, depending on its logic.
CALLED ON NULL INPUT is the default.
- INHERIT SPECIAL REGISTERS
- Specifies that existing values of special registers are inherited upon entry to the function. INHERIT SPECIAL REGISTERS is the default.
- STATIC DISPATCH
- At function resolution time, DB2 chooses a function based on the static (or declared) types of the function parameters. STATIC DISPATCH is the default.
- CARDINALITY integer
- Specifies an estimate of the expected number of rows that the function returns. The number is
used for optimization purposes. The value of integer must be between 0 and
2147483647.
If you do not specify CARDINALITY, DB2 assumes a finite value. The finite value is the same value that DB2 assumes for tables for which the RUNSTATS utility has not gathered statistics.
If a function has an infinite cardinality (the function never returns the end-of-table condition and always returns a row), a query that requires the end-of-table condition to work correctly will need to be interrupted.
- PARAMETER CCSID
- Specifies the encoding scheme for character and graphic string parameters is ASCII, EBCDIC, or UNICODE. The default encoding scheme is the value that is specifies in the CCSID clauses of the parameter list or RETURNS clause, or in the DEF ENCODING SCHEME field on installation panel DSNTIPF. This clause provides a convenient way to specify the encoding scheme for character and graphic string parameters. If individual CCSID clauses are specified for individual parameters in addition to this PARAMETER CCSID clause, the value specified in all of the CCSID clauses must be the same value that is specified in this clause. This clause also specifies the encoding scheme that is used for system-generated parameters of the routine such as message tokens and DBINFO.
NOT SECURED or SECURED
Specifies if the function is considered secure for row access control and column access control. The SECURED or NOT SECURED option applies to all future versions of the function.
- NOT SECURED
- Specifies that the function is not considered secure for row access
control and column access control.
NOT SECURED is the default.
When the function is invoked, the arguments of the function must not reference a column for which a column mask is enabled when the table is using active column access control.
- SECURED
- Specifies that the function is considered secure for row access
control and column access control.
The function must be secure when it is referenced in a row permission or a column mask.
SQL-routine-body
- RETURN-statement
- Specifies the return value of the function. A RETURN statement must be specified for an SQL table function.
- ATOMIC
- ATOMIC indicates that an unhandled exception condition within the RETURN statement causes the statement to be rolled back.
Notes
- Identifier resolution:
See SQL control statements for SQL routines for information on how names are resolved to columns, SQL variables, or SQL parameters within an SQL routine.
If duplicate names are used for columns and SQL variables and parameters, qualify the duplicate names by using the table designator for columns, the routine name for parameters, and the label name for SQL variables.
- Referencing date and time special registers:
- If an SQL function contains multiple references to any of the date or time special registers, all references return the same value. In addition, this value is the same value that is returned by the register invocation in the statement that invoked the function.
- Scrollable cursors specified with user-defined functions:
- A row can be fetched more than once with a scrollable cursor. Therefore, if a scrollable cursor is defined with a function that is not deterministic in the select list of the cursor, a row can be fetched multiple times with different results for each fetch. Similarly, if a scrollable cursor is defined with a user-defined function with external action, the action is executed with every fetch.
- Dependent objects:
- An SQL routine is dependent on objects that are referenced in the routine body.
Considerations for columns that are defined with a field procedure:
The body of an SQL table function must not reference a column that is defined with a field procedure, and the RETURNS clause of an SQL table function must not reference a column that is defined with a field procedure. An SQL table function must not be invoked with an expression that is derived from a column that is defined with a field procedure.
Creating a secure function:
Typically, the security administrator will examine the data that is accessed by a function, ensure that it is secure, and grant the CREATE_SECURE_OBJECT privilege to someone who currently requires the privileges to create a secure user-defined function. After the function is created, they will revoke the CREATE_SECURE_OBJECT privilege from the function owner.
DB2 treats the SECURED attribute as an assertion that declares that the security administrator has established an audit procedure for all changes to the user-defined function. DB2 assumes that such a control audit procedure is in place for all subsequent ALTER FUNCTION statements or changes to external packages. If the function is a compiled SQL function, DB2 assumes that such a control audit procedure is in place for all versions of the function, and that all subsequent ALTER FUNCTION statements or changes to external packages are being reviewed by this audit process.
Invoking other user-defined functions in a secure function:
When a secure user-defined function is referenced in an SQL data change statement that references a table that is using row access control or column access control, and if the secure user-defined function invokes other user-defined functions, the nested user-defined functions are not validated as secure. If those nested functions can access sensitive data, the security administrator needs to ensure that those functions are allowed to access sensitive data and should ensure that a change control audit procedure has been established for all changes to those functions.
The SECURE column in the DSN_FUNCTION_TABLE EXPLAIN table:
The SECURE column in the DSN_FUNCTION_TABLE EXPLAIN table indicates if a user-defined function is considered secure.
- Alternative syntax and synonyms:
- To provide compatibility with previously releases of DB2 or other products int he DB2 family, DB2 supports the following keywords:
- VARIANT as a synonym for NOT DETERMINISTIC
- NOT VARIANT as a synonym for DETERMINISTIC
- NULL CALL as a synonym for CALLED ON NULL INPUT
Examples
Example 1: Define a table function, JTABLE, to return a table with 3 columns:
CREATE FUNCTION JTABLE (COLD_VALUE CHAR(9), T2_FLAG CHAR(1))
RETURNS TABLE (COLA INT, COLB INT, COLC INT)
LANGUAGE SQL
SPECIFIC DEPTINFO
NOT DETERMINISTIC
READS SQL DATA
RETURN
SELECT A.COLA, B.COLB, B.COLC
FROM TABLE1 AS A
LEFT OUTER JOIN
TABLE2 AS B
ON A.COL1 = B.COL1 AND T2_FLAG = 'Y'
WHERE A.COLD = COLD_VALUE;
Example 2: Define a table function that returns the employees in a specified department number. The function simply returns the employees for the requested department:
CREATE FUNCTION DEPTEMPLOYEES (DEPTNO CHAR(3))
RETURNS TABLE (EMPNO CHAR(6), LASTNAME VARCHAR(15), FIRSTNAME VARCHAR(12))
LANGUAGE SQL
READS SQL DATA
NO EXTERNAL ACTION
DETERMINISTIC
RETURN
SELECT EMPNO, LASTNAME, FIRSTNME
FROM YEMP
WHERE YEMP.WORKDEPT = DEPTEMPLOYEES.DEPTNO;
