name ( [ arg_name arg_data_type ] [ , ... ] )
Specifies the identifier (and optionally one or more arguments/inputs) for the UDF. The identifier does not need to be unique for the schema in which the UDF is created because UDFs are identified and resolved by their name and argument types.
However, the identifier must start with an alphabetic character and cannot contain spaces or special characters unless the entire identifier string is enclosed in double quotes (e.g.
"My object"). Identifiers enclosed in double quotes are also case-sensitive.
For more details, see Identifier Syntax.
Specifies the results returned by the UDF, which determines the UDF type:
result_data_type: Creates a scalar UDF that returns a single value with the specified data type.
TABLE ( col_name col_data_type , ... ): Creates a table UDF that returns tabular results with the specified table column(s) and column type(s).
Defines the code executed by the UDF. The contents depend on the type of UDF created:
For more details, see Usage Notes (in this topic).
Specifies that the function is secure. For more information about secure functions, see Secure UDFs.
function_definitionmust contain a SQL expression.
Default: No value (i.e. SQL UDF is created)
CALLED ON NULL INPUTor .
RETURNS NULL ON NULL INPUT | STRICT
Specifies the behavior of the UDF when called with null inputs. In contrast to system-defined functions, which always return null when any input is null, UDFs can handle null inputs, returning non-null values even when an input is null:
CALLED ON NULL INPUTwill always call the UDF with null inputs. It is up to the UDF to handle such values appropriately.
RETURNS NULL ON NULL INPUT(or its synonym
STRICT) will not call the UDF if any input is null. Instead, a null value will always be returned for that row. Note that the UDF may still return null for non-null inputs.
CALLED ON NULL INPUT
VOLATILE | IMMUTABLE
Specifies the behavior of the UDF when returning results:
VOLATILE: UDF may return different values for different rows, even for the same input (e.g. due to non-determinism and statefullness).
IMMUTABLE: UDF assumes that the function, when called with the same inputs, will always return the same result. This guarantee is not checked. Specifying
IMMUTABLEfor a UDF that returns different values for the same input will result in undefined behavior.
COMMENT = 'string_literal'
You can use a character sequence other than one single quote character to delimit the body of the UDF. For example, you can use a sequence of two dollar signs (“$$”). Some of the examples below use “$$” as the delimiter.
If the delimiter for the body of the function is the single quote character, then any single quotes within
function_definition(e.g. string literals) must be escaped by single quotes.
function_definitionhas size restrictions. The maximum allowable size is subject to change.
Create a simple SQL scalar UDF that returns a hard-coded approximation of the mathematical constant pi:
CREATE FUNCTION pi_udf() RETURNS FLOAT AS '3.141592654::FLOAT' ;
Create a simple SQL table UDF that returns hard-coded values:
CREATE FUNCTION simple_table_function () RETURNS TABLE (x INTEGER, y INTEGER) AS $$ SELECT 1, 2 UNION ALL SELECT 3, 4 $$ ;SELECT * FROM TABLE(simple_table_function());
Output:+---+---+ | X | Y | |---+---| | 1 | 2 | | 3 | 4 | +---+---+
Create a SQL table UDF named
get_countries_for_user that returns the results of a query:
CREATE OR REPLACE FUNCTION get_countries_for_user ( id number ) RETURNS TABLE (country_code char, country_name varchar) AS 'select distinct c.country_code, c.country_name from user_addresses a, countries c where a.user_id = id and c.country_code = a.country_code';