Start of change

ALTER FUNCTION (SQL table)

The ALTER FUNCTION (SQL table) statement changes the description of a user-defined SQL table function 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 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:

  • Ownership of the function
  • The ALTERIN privilege on the schema
  • SYSADM authority
  • SYSCTRL authority
  • Start of changeSystem DBADMEnd of change

The authorization ID that matches the schema name implicitly has the ALTERIN privilege on the schema.

Start of changeIf the authorization ID that is used to alter the function has installation SYSADM authority, the function is identified as system-defined function when the function definition is reevaluated.End of change

If a distinct type is referenced (i.e. as the data type of an SQL variable in the body of the function), 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
Start of changeAt least one of the following privileges is required if the SECURED option is specified or if the function is currently secured and the NOT SECURED option is specified:
  • SECADM authority
  • CREATE_SECURE_OBJECT privilege
End of change
Privilege set: If the statement is embedded in an application program, the privilege set is the privileges that are held by the owner of the package.
If the statement is dynamically prepared, the privilege set is the set of privileges that are held by the SQL authorization IDs of the process. The specified routine name can include a schema name (a qualifier). However, if the schema name is not the same as one of these SQL authorization IDs, 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 ALTERIN privilege on the schema
Start of change

Syntax

>>-ALTER--function-designator--RESTRICT--| option-list |-------><

function-designator:

>>-+-FUNCTION--function-name--+----------------------------------+-+-><
   |                          |    .-,----------------------.    | |   
   |                          |    V                        |    | |   
   |                          '-(----+--------------------+-+--)-' |   
   |                                 '-| parameter-type |-'        |   
   |                                                               |   
   |                                                               |   
   '-SPECIFIC FUNCTION--specific-name------------------------------'   

parameter-type:

>>-| data-type |-----------------------------------------------><

data-type:

>>-+-| 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)                                                  
>>-------+-------------------+--+--------------------+---------->
         +-NOT DETERMINISTIC-+  +-EXTERNAL ACTION----+   
         '-DETERMINISTIC-----'  '-NO EXTERNAL ACTION-'   

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

>--+---------------------------+--+-----------------+----------->
   '-INHERIT SPECIAL REGISTERS-'  '-STATIC DISPATCH-'   

>--+----------------------+--+-------------+-------------------><
   '-CARDINALITY--integer-'  +-SECURED-----+   
                             '-NOT SECURED-'   

Notes:
  1. The options in the option-list can be specified in any order. However, the same clause cannot be specified more than one time.
End of change

Description

FUNCTION function-name
Identifies the SQL table function by its function name. The identified function must be an SQL table function.

There must be exactly one function with function-name in the schema. The function can have any number of input parameters. If the schema does not contain a function with function-name, or contains more than one function with this name, an error is returned.

FUNCTION function-name (parameter-type, ...)
Identifies the SQL function by its function signature, which uniquely identifies the function.

A function with the function signature must exist in the explicitly or implicitly specified schema.

function-name
Identifies the function name of the SQL function. If the function was defined with a table parameter (the LIKE TABLE name AS LOCATOR clause was specified in the CREATE FUNCTION statement to indicate that one of the input parameters is a transition table), the function signature cannot be used to uniquely identify the function. Instead, use one of the other syntax variations to identify the function with its function name, if unique, or its specified parameters.

If function-name() is specified, the function that is identified must have zero parameters.

parameter-type
Identifies the number of parameters of the function.
data-type
Identifies the data type of each input parameter of the function. The data type of each parameter must match the data type that was specified in the CREATE FUNCTION statement for the parameter in the corresponding position. The number of data types and the logical concatenation of the data types are used to uniquely identify the function. Therefore , you cannot change the number of parameters or the data types of the parameters.

For data types that have a length, precision, or scale attribute, you can use a set of empty parentheses, specify a value, or accept the default values:

  • Empty parentheses indicate that DB2® is to ignore the attribute when determining whether the data types match.

    For example, DEC() will be considered a match for a parameter of a function defined with a data type of DEC(7,2). Similarly DECFLOAT() will be considered a match for DECFLOAT(16) or DECFLOAT(34).

    FLOAT cannot be specified with empty parentheses because its parameter value indicates different data types (REAL or DOUBLE).

  • If you use a specific value for a length, precision, or scale attribute, the value must exactly match the value that was specified (implicitly or explicitly) in the CREATE FUNCTION statement.

    The specific value for FLOAT(n) does not have to exactly match the defined value of the source function because 1<=n<= 21 indicates REAL and 22<=n<=53 indicates DOUBLE. Matching is based on whether the data type is REAL or DOUBLE.

  • If length, precision, or scale is not explicitly specified and empty parentheses are not specified, the default length of the data type is implied. The implicit length must exactly match the value that was specified (implicitly or explicitly) in the CREATE FUNCTION statement.

For data types with a subtype or encoding scheme attribute, specifying the FOR subtype DATA clause or the CCSID clause is optional. Omission of either clause indicates that DB2 is to ignore the attribute when determining whether the data types match. If you specify either clause, it must match the value that was implicitly or explicitly specified in the CREATE FUNCTION statement.

See CREATE FUNCTION for more information on the specification of the parameter list.

RESTRICT
Start of changeIndicates that the function will not be altered or replaced it if is referenced by any dependent package, function, materialized query table, or view.
Exception: When the ALTER statement specifies only DETERMINISTIC or NOT DETERMINISTIC, the function is altered, and all dependent packages are invalidated.
End of change
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. DB2 uses this information to disable the merging of views and table expressions when processing SELECT and SQL data change statements that refer to this function. 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.
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 uses this information to enable the merging of views and table expressions for SELECT and SQL data change statements that refer to this function.

If applicable, specify DETERMINISTIC to prevent non-optimal access paths from being chosen for SQL statements that refer to this function.

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.
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.
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 argument values are null, making the function responsible for testing for null argument values. The function might return an empty table, depending on the logic in the body of the function.
INHERIT SPECIAL REGISTERS
Specifies that existing values of special registers are inherited upon entry to the function.
STATIC DISPATCH
Specifies that at function resolution time, DB2 chooses a function based on the static (or declared) types of the function parameters.
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 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.

Start of changeSECURED or NOT SECUREDEnd of change
Start of changeSpecifies whether the function is considered secure.
SECURED
Specifies that the function is considered secure.
NOT SECURED
Specifies that the function is considered not secure. NOT SECURED must not be specified when a row permission or a column mask depends on the function.

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.

End of change

Notes

Invalidation of packages:
When an SQL function is changed, all the packages that refer to that function are marked invalid.
Dependent objects:
An SQL routine is dependent on objects that are referenced in the routine body.
Start of changeAltering a function from NOT SECURED to SECURED:End of change
Start of changeTypically, 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 the user that requires privileges to change the user-defined function to be secured. After the function is changed to SECURED, the security administrator will revoke the CREATE_SECURE_OBJECT privilege from the owner of the function.

The function is considered secure after the ALTER FUNCTION statement is executed. 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.

Packages and statements in the dynamic statement cache that reference the function are invalidated.

End of change
Start of changeAltering a function from SECURED to NOT SECURED:End of change
Start of changePackages and statements in the dynamic statement cache that reference the function are invalidated when the function is changed from SECURED to NOT SECURED. An function that is not secured might negatively impact performance if that function accesses data in a table that is using row access control or column access control. To minimize the performance impact, either change the function to use the SECURED option or deactivate row access control or column access control for the table that the function is accessing.End of change
Start of changeInvoking other user-defined functions in a secure function:End of change
Start of changeWhen 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.End of change
Start of changeThe SECURE column in the DSN_FUNCTION_TABLE EXPLAIN table:End of change
Start of changeThe SECURE column in the DSN_FUNCTION_TABLE EXPLAIN table indicates if a user-defined function is considered secure.End of change
Compatibilities:
For compatibility with the CREATE FUNCTION (SQL table) statement, the following clause can be specified, but will be ignored:
  • LANGUAGE SQL
Alternative syntax and synonyms:
To provide compatibility with previously releases of DB2 or other products in the 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: The following statement modifies the definition of an SQL table function to set the estimated cardinality to 10,000.

ALTER FUNCTION GET_TABLE
	RESTRICT CARDINALITY 10000;
End of change