This statement is used to define a user-defined SQL scalar, table or row function. A scalar function returns a single value each time it is invoked, and is generally valid wherever an SQL expression is valid. A table function may be used in a FROM clause and returns a table. A row function may be used as a transform function and returns a row.
Invocation
This statement can be embedded in an application program or issued through the use of dynamic SQL statements. It is an executable statement that can be dynamically prepared. However, if the bind option DYNAMICRULES BIND applies, the statement cannot be dynamically prepared (SQLSTATE 42509).
Authorization
The privileges held by the authorization ID of the statement must include at least one of the following:
If the authorization ID of the statement does not have SYSADM or DBADM authority, and the function identifies a table or view, the privileges that the authorization ID of the statement holds (without considering GROUP privileges) must include SELECT WITH GRANT OPTION for each identified table and view.
If a function definer can only create the function because the definer has SYSADM authority, then the definer is granted implicit DBADM authority for the purpose of creating the function.
If the authorization ID has insufficient authority to perform the operation, an error (SQLSTATE 42502) is raised.
Syntax
>>-CREATE FUNCTION--function-name-------------------------------> >----(--+------------------------------------+---)---*----------> | .-,----------------------------. | | V | | '----parameter-name--data-type1---+--' >----RETURNS--+-data-type2--------------------+--*--------------> '--+-ROW---+---| column-list |--' '-TABLE-' .-LANGUAGE SQL--. >-----+--------------------------+--*----+---------------+--*---> '-SPECIFIC--specific-name--' .-NOT DETERMINISTIC--. .-EXTERNAL ACTION----. >-----+--------------------+--*----+--------------------+--*----> '-DETERMINISTIC------' '-NO EXTERNAL ACTION-' .-READS SQL DATA--. .-STATIC DISPATCH--. >-----+-----------------+--*----+------------------+--*---------> '-CONTAINS SQL----' (1) .-CALLED ON NULL INPUT-------. >-----+----------------------------+--*-------------------------> >-----+-----------------------------------------------------+---> | (2) | '-PREDICATES--(--| predicate-specification |--)-------' >----RETURN--+-expression----------------------------------------------+> +-NULL----------------------------------------------------+ '-+---------------------------------------+---fullselect--' | .-,--------------------------. | | V | | '-WITH-----common-table-expression---+--' >-------------------------------------------------------------->< column-list .-,--------------------------. V | |---(-----column-name--data-type3---+---)-----------------------|
Notes:
Description
The name, including the implicit or explicit qualifiers, together with the number of parameters and the data type of each parameter (without regard for any length, precision or scale attributes of the data type) must not identify a function described in the catalog (SQLSTATE 42723). The unqualified name, together with the number and data types of the parameters, while of course unique within its schema, need not be unique across schemas.
If a two-part name is specified, the schema-name cannot begin with "SYS" (SQLSTATE 42939).
A number of names used as keywords in predicates are reserved for system use, and may not be used as a function-name (SQLSTATE 42939). The names are SOME, ANY, ALL, NOT, AND, OR, BETWEEN, NULL, LIKE, EXISTS, IN, UNIQUE, OVERLAPS, SIMILAR, MATCH and the comparison operators as described in Basic Predicate.
The same name can be used for more than one function if there is some difference in the signature of the functions. Although there is no prohibition against it, an external user-defined table function should not be given the same name as a built-in function.
In this statement, exactly the same considerations apply as for the parameters of SQL functions described above under data-type1 for function parameters.
A row function can only be used as a transform function for a structured type (having one structured type as its parameter and returning only base types).
The specific-name may be the same as an existing function-name.
If no qualifier is specified, the qualifier that was used for function-name is used. If a qualifier is specified, it must be the same as the explicit or implicit qualifier of function-name or an error is raised (SQLSTATE 42882).
If specific-name is not specified, a unique name is generated by the database manager. The unique name is SQL followed by a character timestamp, SQLyymmddhhmmssxxx.
NOT DETERMINISTIC must be explicitly or implicitly specified if the body of the function accesses a special register or calls another non-deterministic function (SQLSTATE 428C2).
EXTERNAL ACTION must be explicitly or implicitly specified if the body of the function calls another function that has an external action (SQLSTATE 428C2).
The phrase NULL CALL may be used in place of CALLED ON NULL INPUT.
If the function is a scalar function, the fullselect must return one column (SQLSTATE 42823) and, at most, one row (SQLSTATE 21000).
If the function is a row function, it must return, at most, one row (SQLSTATE 21505).
If the function is a table function, it can return zero or more rows with one or more columns.
Notes
For the purpose of comparing parameter-types in the above rules:
Examples
Example 1: Define a scalar function that returns the tangent of a value using the existing sine and cosine functions.
CREATE FUNCTION TAN (X DOUBLE) RETURNS DOUBLE LANGUAGE SQL CONTAINS SQL NO EXTERNAL ACTION DETERMINISTIC RETURN SIN(X)/COS(X)
Example 2: Define a transform function for the structured type PERSON.
CREATE FUNCTION FROMPERSON (P PERSON) RETURNS ROW (NAME VARCHAR(10), FIRSTNAME VARCHAR(10)) LANGUAGE SQL CONTAINS SQL NO EXTERNAL ACTION DETERMINISTIC RETURN VALUES (P..NAME, P..FIRSTNAME)
Example 3: Define a table function that returns the employees in a specified department number.
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 EMPLOYEE WHERE EMPLOYEE.WORKDEPT = DEPTEMPLOYEES.DEPTNO
Note that the definer of this function must have the SELECT WITH GRANT OPTION privilege on the EMPLOYEE table and that all users may invoke the table function DEPTEMPLOYEES, effectively giving them access to the data in the result columns for each department number.