The privilege set defined below must include at least one of the following:
The authorization ID that matches the schema name implicitly has the CREATEIN privilege on the schema.
Bold text indicates clauses or options that are supported only syntactically.
CREATE FUNCTION function-name ([parameter-declaration[,...]]) RETURNS {data-type2 [AS LOCATOR] | data-type3 CAST FROM data-type4 [AS LOCATOR]} option-list
option-list:
[SPECIFIC specific-name] [EXTERNAL [NAME {'string' | identifier}] LANGUAGE {ASSEMBLE | C | COBOL | PLI}] [PARAMETER STYLE DB2SQL [DETERMINISTIC | NOT DETERMINISTIC] [FENCED]] [RETURNS NULL ON NULL INPUT | CALLED ON NULL INPUT] [READS SQL DATA | NO SQL | MODIFIES SQL DATA | CONTAINS SQL] [EXTERNAL ACTION | NO EXTERNAL ACTION] [NO SCRATCHPAD | SCRATCHPAD [100 | length]] [FINAL CALL | NO FINAL CALL] [ALLOW PARALLEL | DISALLOW PARALLEL] [DBINFO | NODBINFO] [NO COLLID | COLLID collection-id] [WLM ENVIRONMENT {name | (name)}] [ASUTIME NO LIMIT | ASUTIME LIMIT integer] [STAY RESIDENT NO | STAY RESIDENT YES] [PROGRAM TYPE SUB | PROGRAM TYPE MAIN] [SECURITY DB2 | SECURITY {USER | DEFINED}] [STOP AFTER SYSTEM DEFAULT FAILURES | STOP AFTER integer FAILURES | CONTINUE AFTER FAILURE] [INHERIT SPECIAL REGISTERS | DEFAULT SPECIAL REGISTERS] [RUN OPTIONS run-time-options]
parameter-declaration:
[parameter-name] parameter-type
parameter-type:
{data-type [AS LOCATOR] | TABLE LIKE {table-name | view-name} [AS LOCATOR]}
data-type:
{built-in-data-type | distinct-type-name}
built-in-data-type:
SMALLINT | {INTEGER | INT} | {DECIMAL | DEC | NUMERIC} [integer[,...]] | {FLOAT [integer] | REAL [PRECISION] | DOUBLE} | { {CHARACTER | CHAR} [(integer)] | {CHARACTER | CHAR} VARYING (integer)| VARCHAR (integer) } [FOR {SBCS | MIXED | BIT} DATA] [CCSID {EBCDIC | ASCII}] | {{CHARACTER | CHAR} LARGE OBJECT | CLOB} [(integer [K|M|G])] } [FOR {SBCS | MIXED} DATA] [CCSID {EBCDIC | ASCII}] } | {BINARY LARGE OBJECT | BLOB} (integer [K|M|G]) | {GRAPHIC [(integer)] | VARGRAPHIC (integer) | DBCLOB (integer [K|M|G])} [CCSID {EBCDIC | ASCII}] | {DATE | TIME | TIMESTAMP} | ROWID
Names the user-defined function. The name is implicitly or explicitly qualified by a schema name. The combination of name, schema name, the number of parameters, and the data type of each parameter (without regard for any length, precision, scale, subtype or encoding scheme attributes of the data type) must not identify a user-defined function that exists at the current server.
You can use the same name for more than one function if the function signature of each function is unique.
The unqualified form of function-name is a long SQL identifier. The name must not be any of the following system-reserved keywords even if you specify them as delimited identifiers:
The unqualified function name is implicitly qualified with a schema name according to the following rules:
The qualified form of function-name is a short SQL identifier (the schema name) followed by a period and a long SQL identifier.
The schema name can be 'SYSTOOLS' if the user who executes the CREATE statement has SYSADM or SYSCTRL privilege. Otherwise, the schema name must not begin with 'SYS' unless the schema name is 'SYSADM'.
The owner of the function is determined by how the CREATE FUNCTION statement is invoked:
The owner is implicitly given the EXECUTE privilege with the GRANT option for the function.
Identifies the number of input parameters of the function, and specifies the data type of each parameter. All the parameters for a function are input parameters. There must be one entry in the list for each parameter that the function expects to receive. Although not required, you can give each parameter a name.
A function can have no parameters. In this case, you must code an empty set of parentheses, for example:
CREATE FUNCTION WOOFER()
Specifies the name of the input parameter. The name is a long SQL identifier, and each name in the parameter list must not be the same as any other name.
Specifies the data type of the input parameter. The data type can be a built-in data type or a distinct type.
ALL | LIKE | UNIQUE |
AND | MATCH | UNKNOWN |
ANY | NOT | = |
BETWEEN | NULL | ¬= |
DISTINCT | ONLY | < |
EXCEPT | OR | <= |
EXISTS | OVERLAPS | ¬< |
FALSE | SIMILAR | > |
FOR | SOME | >= |
FROM | TABLE | ¬> |
IN | TRUE | <> |
IS | TYPE | |
built-in-data-type | The data type of the input parameter is a built-in data type. You can use the same built-in data types as for the CREATE TABLE statement except LONG VARCHAR or LONG VARGRAPHIC. Use VARCHAR or VARGRAPHIC with an explicit length instead.
If you do not specify a specific value for the data types that have length, precision, or scale attributes (CHAR, GRAPHIC, DECIMAL, NUMERIC, FLOAT), the defaults are as follows: |
distinct-type-name | The data type of the input parameter is a distinct type. Any length, precision, scale, subtype, or encoding scheme attributes for the parameter are those of the source type of the distinct type. |
data-type2 | Specifies the data type of the output. |
AS LOCATOR | Specifies that the function returns a locator to the value rather than the actual value. You can specify AS LOCATOR only if data-type4 is a LOB data type or a distinct type based on a LOB data type. |
data-type3 CAST FROM data-type4 | Specifies the data type of the output of the function (data-type4) and the data type in which that output is returned to the invoking statement (data-type3). The two data types can be different For example, for the following definition, the function returns a DOUBLE value, which DB2 converts to a DECIMAL value and then passes to the statement that invoked the function:
CREATE FUNCTION SQRT(DECIMAL(15,0)) RETURNS DECIMAL(15,0) CAST FROM DOUBLE |
ASSEMBLE | The function is written in Assembler. (Not currently available with XDB Server.) |
C | The function is written in C or C++. (Not currently available with XDB Server.) |
COBOL | The function is written in COBOL, including the object-oriented language extensions. |
PLI | The function is written in PL/I. (Not currently available with XDB Server.) |
NOT DETERMINISTIC | The function might not return the same result for identical input arguments. The function depends on some state values that affect the results. XDB uses this information when processing a SELECT, UPDATE, DELETE, or INSERT statement to disable merging of views that refer to the function. An example of a function that is not deterministic is one that generates random numbers, or any function that contains SQL statements.
NOT DETERMINISTIC is the default. Some functions that are not deterministic can receive incorrect results if the function is executed by parallel tasks. Specify the DISALLOW PARALLEL clause for these functions. |
DETERMINISTIC | The function always returns the same result for identical input arguments. XDB can use this information to optimize view processing for SELECT, UPDATE, DELETE, or INSERT statements. An example of a deterministic function is a function that calculates the square root of the input.
DB2 does not verify that the function program is consistent with the specification of DETERMINISTIC or NOT DETERMINISTIC. |
RETURNS NULL ON INPUT | The function is not called if any of the input arguments is null. The result is the null value. |
CALLED ON NULL INPUT | The function is called regardless of whether any of the input arguments is null, making the function responsible for testing for null argument values. The function can return a null or nonnull value. |
NO SQL | The function cannot execute any SQL statements. |
MODIFIES SQL DATA | The function can execute any SQL statement except those statements that are not supported in any function. Do not specify MODIFIES SQL DATA when ALLOW PARALLEL is in effect. |
READS SQL DATA | The function cannot execute SQL statements that modify data. SQL statements that are not supported in any function return a different error.
READS SQL DATA is the default. |
CONTAINS SQL | The function cannot execute any SQL statements that read or modify data. SQL statements that are not supported in any function return a different error. |
NO SCRATCHPAD |
A scratchpad is not allocated and passed to the function. NO SCRATCHPAD is the default. |
SCRATCHPAD length |
When the function is invoked for the first time, XDB allocates memory for a scratchpad. |
NO FINAL CALL | A final call is not made to the external scalar function. The function does not receive an additional argument that specifies the type of call. |
FINAL CALL | A final call is made to the external scalar function. See the following description of call types for the characteristics of a final call. When FINAL CALL is specified, the function receives an additional argument that specifies the type of call to enable the function to differentiate between a final call and another type of call. |
NO DBINFO | Additional information is not passed. |
DBINFO | An additional argument is passed when the function is invoked. The argument is a structure that contains information such as the application run-time authorization ID, the schema name, the name of a table or column that the function might be inserting into or updating, and identification of the database server that invoked the function. For details about the argument and its structure, see your DB2 Application Programming and SQL Guide. |
NO COLLID | The package collection for the function is the same as the package collection of the program that invokes the function. If a trigger invokes the function, the collection of the trigger package is used. If the invoking program does not use a package, XDB resolves the package by using the CURRENT PACKAGE PATH special register or the CURRENT PACKAGESET special register. |
COLLID collection-id | The name of the package collection that is used when the function is executed. |
NO LIMIT | There is no limit on the service units. NO LIMIT is the default. |
LIMIT integer | The limit on the service units is a positive integer in the range of 1 to 2GB. If the function uses more service units than the specified value, the function is canceled. |
NO | The load module is deleted from memory after the function ends. Use NO for non-reentrant functions. NO is the default. |
YES | The load module remains resident in memory after the function ends. Use YES for reentrant functions. |
SUB | The function runs as a subroutine. SUB is the default. |
MAIN | The function runs as a main routine. |
Parameters with a datetime data type or a distinct type are passed to the function as a different data type:
Specifies that a locator to the value of the parameter is passed to the function instead of the actual value. Specify AS LOCATOR only for parameters with a LOB data type or a distinct type based on a LOB data type. Passing locators instead of values can result in fewer bytes being passed to the function, especially when the value of the parameter is very large.
The AS LOCATOR clause has no effect on determining whether data types can be promoted, nor does it affect the function signature, which is used in function resolution.
The XDB Server operating environment does not support the LOB data type at this time.
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 single value is a locator to the table, which the function uses to access the columns of the transition table. A function with a table parameter can only be invoked from the triggered action of a trigger.
The use of TABLE LIKE provides an implicit definition of the transition table. It specifies that the transition table has the same number of columns as the identified table or view. The columns have the same data type, length, precision, scale, subtype, and encoding scheme as the identified table or view, as they are described in catalog tables SYSCOLUMNS and SYSTABLESPACES.
The name specified after TABLE LIKE must identify a table or view that exists at the current server. The name must not identify a declared temporary table. The name does not have to be the same name as the table that is associated with the transition table for the trigger. An unqualified table or view name is implicitly qualified according to the following rules:
When the function is invoked, the corresponding columns of the transition table identified by the table locator and the table or view identified in the TABLE LIKE clause must have the same definition. The data type, length, precision, scale, and encoding scheme of these columns must match exactly. The description of the table or view at the time the CREATE FUNCTION statement was executed is used.
XDB Server operating environment does not support transition table parameters at this time.
Identifies the output of the function. Consider this clause in conjunction with the optional CAST FROM clause.
data-type2 | Specifies the data type of the output. |
AS LOCATOR | Specifies that the function returns a locator to the value rather than the actual value. You can specify AS LOCATOR only if data-type4 is a LOB data type or a distinct type based on a LOB data type. |
data-type3 CAST FROM data-type4 | Specifies the data type of the output of the function (data-type4) and the data type in which that output is returned to the invoking statement (data-type3). The two data types can be different For example, for the following definition, the function returns a DOUBLE value, which DB2 converts to a DECIMAL value and then passes to the statement that invoked the function:
CREATE FUNCTION SQRT(DECIMAL(15,0)) RETURNS DECIMAL(15,0) CAST FROM DOUBLE |
The value of data-type4 must not be a distinct type and must be castable to data-type3. The value for data-type3 can be any built-in data type or distinct type.
Specifies a unique name for the function. The name is implicitly or explicitly qualified with a schema name. The name, including the schema name, must not identify the specific name of another function that exists at the current server.
The unqualified form of specific-name is a long SQL identifier. The qualified form is a short SQL identifier (the schema name) followed by a period and a long SQL identifier.
If you do not specify a schema name, it is the same as the explicit or implicit schema name of the function name (function-name). If you specify a schema name, it must be the same as the explicit or implicit schema name of the function name.
If you do not specify the SPECIFIC clause, the default specific name is the name of the function. However, if the function name does not provide a unique specific name or if the function name is a single asterisk , XDB generates a specific name in the form of:
SQLxxxxxxxxxxxx
where xxxxxxxxxxxx is a string of 12 characters that make the name unique.
The specific name is stored in the SPECIFIC column of the SYSROUTINES catalog table. The specific name can be used to uniquely identify the function in several SQL statements (such as ALTER FUNCTION, COMMENT ON, DROP, GRANT, and REVOKE). However, the function cannot be invoked by its specific name.
Specifies that the function being registered is based on code that is written in an external programming language and adheres to the documented linkage conventions and interface of that language.
If you do not specify the NAME clause, 'NAME function-name' is implicit. In this case, function-name must not be longer than 8 characters.
Identifies the name of the MVS load module that contains the user-written code that implements the logic of the function. The name can be a string constant that is no longer than 8 characters or a short identifier, The name must conform to the naming conventions for MVS load modules.
DB2 loads the load module when the function is invoked. The load module is created when the program that contains the function body is compiled and link-edited. The load module does not need to exist when the CREATE FUNCTION statement is executed. However, it must exist and be accessible by the current server when the function is invoked.
You can specify the EXTERNAL clause in one of the following ways:
EXTERNAL EXTERNAL NAME PKJVSP1 EXTERNAL NAME 'PKJVSP1'
If you specify an external program name, you must use the NAME keyword. For example, this syntax is not valid:
EXTERNAL PKJVSP1
Specifies the application programming language in which the function program is written. All programs must be designed to run in IBM's Language Environment environment.
ASSEMBLE | The function is written in Assembler. (Not currently available with XDB Server.) |
C | The function is written in C or C++. (Not currently available with XDB Server.) |
COBOL | The function is written in COBOL, including the object-oriented language extensions. |
PLI | The function is written in PL/I. (Not currently available with XDB Server.) |
Specifies the linkage convention that the function program uses to receive input parameters from and pass return values to the invoking SQL statement.
DB2SQL indicates that parameters for indicator variables are associated with each input and return value to allow for null values. The parameters that are passed between the invoking SQL statement and the function include:
Zero to three additional parameters might also be passed:
Specifies whether the function returns the same results for identical input arguments.
NOT DETERMINISTIC | The function might not return the same result for identical input arguments. The function depends on some state values that affect the results. XDB uses this information when processing a SELECT, UPDATE, DELETE, or INSERT statement to disable merging of views that refer to the function. An example of a function that is not deterministic is one that generates random numbers, or any function that contains SQL statements.
NOT DETERMINISTIC is the default. Some functions that are not deterministic can receive incorrect results if the function is executed by parallel tasks. Specify the DISALLOW PARALLEL clause for these functions. |
DETERMINISTIC | The function always returns the same result for identical input arguments. XDB can use this information to optimize view processing for SELECT, UPDATE, DELETE, or INSERT statements. An example of a deterministic function is a function that calculates the square root of the input.
DB2 does not verify that the function program is consistent with the specification of DETERMINISTIC or NOT DETERMINISTIC. |
Specifies whether the function is called if any of the input arguments is null at execution time.
RETURNS NULL ON INPUT | The function is not called if any of the input arguments is null. The result is the null value. |
CALLED ON NULL INPUT | The function is called regardless of whether any of the input arguments is null, making the function responsible for testing for null argument values. The function can return a null or nonnull value. |
Indicates whether the function can execute any SQL statements and, if so, what type.
NO SQL | The function cannot execute any SQL statements. |
MODIFIES SQL DATA | The function can execute any SQL statement except those statements that are not supported in any function. Do not specify MODIFIES SQL DATA when ALLOW PARALLEL is in effect. |
READS SQL DATA | The function cannot execute SQL statements that modify data. SQL statements that are not supported in any function return a different error.
READS SQL DATA is the default. |
CONTAINS SQL | The function cannot execute any SQL statements that read or modify data. SQL statements that are not supported in any function return a different error. |
This clause has no functional equivalent in the XDB Server operating environment.
Specifies whether XDB provides a scratchpad for the function. It is strongly recommended that external functions be reentrant, and a scratchpad provides an area for the function to save information from one invocation to the next.
A scratchpad has the following characteristics:
SELECT A, UDFX(A) FROM TABLEB WHERE UDFX(A) > 103 OR UDFX(A) < 19;
If the function is run under parallel tasks, one scratchpad is allocated for each parallel task of each reference to the function in the SQL statement. This can lead to unpredictable results. For example, if a function uses the scratchpad to count the number of times that it is invoked, the count reflects the number of invocations done by the parallel task and not the SQL statement. Specify the DISALLOW PARALLEL clause for functions that will not work correctly with parallelism.
Each time the function invoked, XDB passes an additional argument to the function that contains the address of the scratchpad.
NO FINAL CALL | A final call is not made to the external scalar function. The function does not receive an additional argument that specifies the type of call. |
FINAL CALL | A final call is made to the external scalar function. See the following description of call types for the characteristics of a final call. When FINAL CALL is specified, the function receives an additional argument that specifies the type of call to enable the function to differentiate between a final call and another type of call. |
This clause has no functional equivalent in the XDB Server operating environment.
Specifies whether specific information that XDB knows is passed to the function when it is invoked.
NO DBINFO | Additional information is not passed. |
DBINFO | An additional argument is passed when the function is invoked. The argument is a structure that contains information such as the application run-time authorization ID, the schema name, the name of a table or column that the function might be inserting into or updating, and identification of the database server that invoked the function. For details about the argument and its structure, see your DB2 Application Programming and SQL Guide. |
Identifies the package collection that is used when the function is executed.
NO COLLID | The package collection for the function is the same as the package collection of the program that invokes the function. If a trigger invokes the function, the collection of the trigger package is used. If the invoking program does not use a package, XDB resolves the package by using the CURRENT PACKAGE PATH special register or the CURRENT PACKAGESET special register. |
COLLID collection-id | The name of the package collection that is used when the function is executed. |
Specifies the total amount of processor time, in CPU service units, that a single invocation of the function can run. The value is unrelated to the ASUTIME column of the resource limit specification table.
When you are debugging a function, setting a limit can be helpful if the function gets caught in a loop.
The XDB Server operating environment only provides NO LIMIT at this time.
Specifies whether the load module for the function remains resident in memory when the function ends.
The XDB Server provides an operating environment where the function does not remain resident.
Specifies whether the routine is to be put in a stopped state after some number of failures.
This clause is only implemented syntactically in the XDB Server operating environment.
This clause has no functional equivalent in the XDB Server operating environment.
When you choose the data types of the input and output parameters for your function, consider the rules of promotion that can affect the values of the parameters. For example, a constant that is one of the input arguments to the function might have a built-in data type that is different from the data type that the function expects, and more significantly, might not be promotable to that expected data type. Based on the rules of promotion, using the following data types for parameters is recommended:
For portability of functions across platforms that are not DB2 for OS/390, do not use the following data types, which might have different representations on different platforms:
At the current server, the function signature of each function, which is the qualified function name combined with the number and data types of the input parameters, must be unique. If the function has more than 30 input parameters, only the data types of the first 30 are used to determine uniqueness. This means that two different schemas can each contain a function with the same name that have the same data types for all of their corresponding data types. However, a single schema must not contain multiple functions with the same name that have the same data types for all of their corresponding data types.
When determining whether corresponding data types match, XDB does not consider any length, precision, scale, subtype or encoding scheme attributes in the comparison. XDB considers the synonyms of data types (DECIMAL and NUMERIC, REAL and FLOAT, and DOUBLE and FLOAT) a match. Therefore, CHAR(8) and CHAR(35) are considered to be the same, as are DECIMAL(11,2), DECIMAL(4,3), and NUMERIC(4,2).
Assume that the following statements are executed to create four functions in the same schema. The second and fourth statements fail because they create functions that are duplicates of the functions that the first and third statements created.
CREATE FUNCTION PART (INT, CHAR(15)) ... CREATE FUNCTION PART (INTEGER, CHAR(40)) ... CREATE FUNCTION ANGLE (DECIMAL(12,2)) ... CREATE FUNCTION ANGLE (DEC(10,7)) ...
Giving an external function the same name as a built-in function is not a recommended practice unless you are trying to change the functionality of the built-in function.
If you do intend to create an external function with the same name as a built-in function, be careful to maintain the uniqueness of its function signature. If your function has the same name and data types of the corresponding parameters of the built-in function but implements different logic, XDB might choose the wrong function when the function is invoked with an unqualified function name. Thus, the application might fail, or perhaps even worse, run successfully but provide an inappropriate result.
Assume that you want to write an external function program in C that implements the following logic:
output = 2 * input – 4
The function should return a null value if and only if one of the input arguments is null. The simplest way to avoid a function call and get a null result when an input value is null is to specify RETURNS NULL ON NULL INPUT on the CREATE FUNCTION statement or allow it to be the default.
Write the statement needed to register the function, using the specific name MINENULL1.
CREATE FUNCTION NTEST1 (SMALLINT) RETURNS SMALLINT EXTERNAL NAME 'NTESTMOD' SPECIFIC MINENULL1 LANGUAGE C DETERMINISTIC NO SQL FENCED PARAMETER STYLE DB2SQL RETURNS NULL ON NULL INPUT NO EXTERNAL ACTION;
Assume that user Smith wants to register an external function named CENTER in schema SMITH. The function program will be written in C and will be reentrant. Write the statement that Smith needs to register the function, letting DB2 generate a specific name for the function.
CREATE FUNCTION CENTER (INTEGER, FLOAT) RETURNS FLOAT EXTERNAL NAME 'MIDDLE' LANGUAGE C DETERMINISTIC NO SQL FENCED PARAMETER STYLE DB2SQL NO EXTERNAL ACTION STAY RESIDENT YES;
Assume that user McBride (who has administrative authority) wants to register an external function named CENTER in the SMITH schema. McBride plans to give the function specific name FOCUS98. The function program uses a scratchpad to perform some one-time only initialization and save the results. The function program returns a value with a FLOAT data type. Write the statement McBride needs to register the function and ensure that when the function is invoked, it returns a value with a data type of DECIMAL(8,4).
CREATE FUNCTION SMITH.CENTER (FLOAT, FLOAT, FLOAT) RETURNS DECIMAL(8,4) CAST FROM FLOAT EXTERNAL NAME 'CMOD' SPECIFIC FOCUS98 LANGUAGE C DETERMINISTIC NO SQLFENCED PARAMETER STYLE DB2SQL NO EXTERNAL ACTION SCRATCHPAD NO FINAL CALL;