ALTER FUNCTION statement (inlined SQL scalar function)
The ALTER FUNCTION (inlined SQL scalar) statement changes the description of a user-defined inlined SQL scalar function at the current server.
Invocation for ALTER FUNCTION (inlined SQL scalar)
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 in effect. For more information, see Authorization IDs and dynamic SQL.
Authorization for ALTER FUNCTION (inlined SQL scalar)
- Ownership of the function
- The ALTERIN privilege on the schema
- SYSADM authority
- SYSCTRL authority
- System DBADM
Installation SYSOPR authority (when the current SQLID of the process is set to SYSINSTL)
The authorization ID that matches the schema name implicitly has the ALTERIN privilege on the schema.
If the authorization ID that is used to
alter the function has the installation SYSADM authority or the installation
SYSOPR authority and if the current SQLID is set to SYSINSTL, the
function is identified as system-defined function when the function
definition is reevaluated.
Additional privileges might be required in the following situations:
- If SQL-routine-body is specified, the privilege set must include the privileges that are required to execute the statements in SQL-routine-body.
- If a user-defined type is referenced (for example, as the data
type of a parameter), the privilege set must include at least one
of the following:
- Ownership of the user-defined type
- The USAGE privilege on the user-defined type
- SYSADM authority
- SECADM authority
- CREATE_SECURE_OBJECT privilege
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.
- 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
Syntax for ALTER FUNCTION (inlined SQL scalar)
parameter-type:
data-type:
built-in-type:
option-list: (Specify options in any order. Specify at least one option. Do not specify the same option more than one time.)
Description for ALTER FUNCTION (inlined SQL scalar)
One of the following three clauses identifies the function to be changed.
- FUNCTION function-name
- Identifies the SQL function by its function name.
The identified function must be an inlined SQL scalar function. There must be exactly one function with function-name in the schema. The function can have any number of input parameters.1 If the schema does not contain a function with function-name or contains more than one function with this name, an error occurs.
The function must not be obfuscated.
- FUNCTION function-name (parameter-type,...)
- Identifies the SQL function by its function signature, which uniquely
identifies the function.
- function-name
- Gives the function name of the inlined SQL scalar function.
If function-name() is specified, the function that is identified must have zero parameters.
- (parameter-type,...)
- Specifies the number of input parameters of the function and the name and data type of each parameter.
- (data-type,...)
- Identifies the number of input parameters of the function and
the data type of each parameter. 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 and22<=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.
- Empty parentheses indicate that Db2 is
to ignore the attribute when determining whether the data types match.
See CREATE FUNCTION statement (overview) for more information on the specification of the parameter list.
A function with the function signature must exist in the explicitly or implicitly specified schema.
- SPECIFIC FUNCTION specific-name
- Identifies a particular user-defined function by its specific name. The name is implicitly or explicitly qualified with a schema name. An inlined SQL scalar function with the specific name must exist in the schema. If the specific name is not qualified, it is implicitly qualified with a schema name as described in the description for FUNCTION function-name.
- NOT DETERMINISTIC or DETERMINISTIC
- Specifies
whether the function returns the same results each time that the function
is invoked with the same input arguments.
- NOT DETERMINISTIC
- The function might not return the same result each time that the
function is invoked with the same input arguments. The function depends
on some state values that affect the results. Db2 uses this information to disable the merging
of views and table expressions when processing SELECT or SQL data
change statements that refer to this function. An example of a function
that is not deterministic is one that generates random numbers.
NOT DETERMINISTIC must be specified explicitly or implicitly if the function program accesses a special register or invokes another function that is not deterministic.
- DETERMINISTIC
- The function always returns the same result each time that the function is invoked with the same input arguments. An example of a deterministic function is a function that calculates the square root of the input. Db2 uses this information to enable the merging of views and table expressions for SELECT or 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 takes an action that changes the state of an
object that Db2 does not manage.
An example of an external action is sending a message or writing a
record to a file.
- EXTERNAL ACTION
- The function can take an action that changes the state of an object
that Db2 does not manage.
Some SQL statements that invoke functions with external actions can result in incorrect results if parallel tasks execute the function. For example, if the function sends a note for each initial call to it, one note is sent for each parallel task instead of once for the function.
If you specify EXTERNAL ACTION, Db2:
- Materializes the views and table expressions in SELECT or SQL data change statements that refer to the function. This materialization can adversely affect the access paths that are chosen for the SQL statements that refer to this function. Do not specify EXTERNAL ACTION if the function does not have an external action.
- Does not move the function from one task control block (TCB) to another between FETCH operations.
- Does not allow another function or stored procedure to use the TCB until the cursor is closed. This is also applicable for cursors declared WITH HOLD.
The only changes to resources made outside of Db2 that are under the control of commit and rollback operations are those changes made under RRS control.
EXTERNAL ACTION must be specified implicitly or explicitly specified if the SQL routine body invokes a function that is defined with EXTERNAL ACTION.
- NO EXTERNAL ACTION
- The function does not take any action that changes the state of an object that Db2 does not manage. Db2 uses this information to enable the merging of views and table expressions for SELECT or SQL data change statements that refer to this function. If applicable, specify NO EXTERNAL ACTION to prevent non-optimal access paths from being chosen for SQL statements that refer to this function.
Db2 does not verify that the function program is consistent with the specification of EXTERNAL ACTION or NO EXTERNAL ACTION.
- 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 classification of READS
SQL DATA, CONTAINS SQL, or NO 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 classification of CONTAINS SQL or NO SQL. The function cannot execute SQL statements the read or modify data.
- STATIC DISPATCH
- At function resolution time, Db2 chooses a function based on the static (or declared) types of the function parameters.
- CALLED ON NULL INPUT
- The function is called regardless of whether any of the input arguments are null, making the function responsible for testing for null arguments. The function can return null.
- SECURED or NOT SECURED
- Specifies if the function is considered secure.
- SECURED
- Specifies that the function is considered secure.
Use of this clause or keyword might invalidate packages that depend on the target object, or packages that depend on related objects through cascading effects. See Changes that invalidate packages.
- 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.
Use of this clause or keyword might invalidate packages that depend on the target object, or packages that depend on related objects through cascading effects. See Changes that invalidate packages.
Notes for ALTER FUNCTION (inlined SQL scalar)
- ALTER FUNCTION for in use functions:
- ALTER FUNCTION will be locked out from making changes if the function is in use. This wait for completion behavior happens even if the query invokes the function multiple times for processing multiple rows or if the query contains multiple references to the function that is being changed.
- Invalidation of plans and packages:
- When an inlined SQL function is altered, all the plans and 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.
- Altering a function from NOT SECURED to SECURED:
- 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 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.
- Altering a function from SECURED to NOT SECURED:
- Packages 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.
- 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.
Altering obfuscated functions:
Obfuscated functions cannot be altered in any way.
- Compatibilities:
- For compatibility with the CREATE FUNCTION (SQL scalar) statement,
the following clause can be specified, but will be ignored:
- LANGUAGE SQL
- Optional syntax:
- To provide compatibility with the syntax of the CREATE FUNCTION
statement, the following options can also be specified:
- SPECIFIC
- PARAMETER CCSID
However, if these options are specified, the value for the option must be the same as the value that is already in effect for the function.
- Alternative syntax and synonyms:
- To provide compatibility with previous 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 for ALTER FUNCTION (inlined SQL scalar)
ALTER FUNCTION MY_UDF1
DETERMINISTIC;