CREATE FUNCTION v11
Name
CREATE FUNCTION
-- define a new function.
Synopsis
Description
CREATE FUNCTION
defines a new function. CREATE OR REPLACE FUNCTION
will either create a new function, or replace an existing definition.
If a schema name is included, then the function is created in the specified schema. Otherwise it is created in the current schema. The name of the new function must not match any existing function with the same input argument types in the same schema. However, functions of different input argument types may share a name (this is called overloading). (Overloading of functions is an Advanced Server feature - overloading of stored, standalone functions is not compatible with Oracle databases.)
To update the definition of an existing function, use CREATE OR REPLACE FUNCTION
. It is not possible to change the name or argument types of a function this way (if you tried, you would actually be creating a new, distinct function). Also, CREATE OR REPLACE FUNCTION
will not let you change the return type of an existing function. To do that, you must drop and recreate the function. Also when using OUT
parameters, you cannot change the types of any OUT
parameters except by dropping the function.
The user that creates the function becomes the owner of the function.
Parameters
name
name
is the identifier of the function.
parameters
parameters
is a list of formal parameters.
data_type
data_type
is the data type of the value returned by the function’s RETURN
statement.
declarations
declarations
are variable, cursor, type, or subprogram declarations. If subprogram declarations are included, they must be declared after all other variable, cursor, and type declarations.
statements
statements
are SPL program statements (the BEGIN - END
block may contain an EXCEPTION
section).
IMMUTABLE
STABLE
VOLATILE
These attributes inform the query optimizer about the behavior of the function; you can specify only one choice. VOLATILE
is the default behavior.
IMMUTABLE
indicates that the function cannot modify the database and always reaches the same result when given the same argument values; it does not do database lookups or otherwise use information not directly present in its argument list. If you include this clause, any call of the function with all-constant arguments can be immediately replaced with the function value.STABLE
indicates that the function cannot modify the database, and that within a single table scan, it will consistently return the same result for the same argument values, but that its result could change across SQL statements. This is the appropriate selection for function that depend on database lookups, parameter variables (such as the current time zone), etc.VOLATILE
indicates that the function value can change even within a single table scan, so no optimizations can be made. Please note that any function that has side-effects must be classified volatile, even if its result is quite predictable, to prevent calls from being optimized away.
DETERMINISTIC
DETERMINISTIC
is a synonym for IMMUTABLE
. A DETERMINISTIC
function cannot modify the database and always reaches the same result when given the same argument values; it does not do database lookups or otherwise use information not directly present in its argument list. If you include this clause, any call of the function with all-constant arguments can be immediately replaced with the function value.
[ NOT ] LEAKPROOF
A LEAKPROOF
function has no side effects, and reveals no information about the values used to call the function.
CALLED ON NULL INPUT
RETURNS NULL ON NULL INPUT
STRICT
CALLED ON NULL INPUT
(the default) indicates that the procedure will be called normally when some of its arguments areNULL
. It is the author's responsibility to check forNULL
values if necessary and respond appropriately.RETURNS NULL ON NULL INPUT
orSTRICT
indicates that the procedure always returnsNULL
whenever any of its arguments areNULL
. If these clauses are specified, the procedure is not executed when there areNULL
arguments; instead aNULL
result is assumed automatically.
[ EXTERNAL ] SECURITY DEFINER
SECURITY DEFINER
specifies that the function will execute with the privileges of the user that created it; this is the default. The key word EXTERNAL
is allowed for SQL conformance, but is optional.
[ EXTERNAL ] SECURITY INVOKER
The SECURITY INVOKER
clause indicates that the function will execute with the privileges of the user that calls it. The key word EXTERNAL
is allowed for SQL conformance, but is optional.
AUTHID DEFINER
AUTHID CURRENT_USER
The
AUTHID DEFINER
clause is a synonym for[EXTERNAL] SECURITY DEFINER
. If theAUTHID
clause is omitted or ifAUTHID DEFINER
is specified, the rights of the function owner are used to determine access privileges to database objects.The
AUTHID CURRENT_USER
clause is a synonym for[EXTERNAL] SECURITY INVOKER
. IfAUTHID CURRENT_USER
is specified, the rights of the current user executing the function are used to determine access privileges.
PARALLEL { UNSAFE | RESTRICTED | SAFE }
The PARALLEL
clause enables the use of parallel sequential scans (parallel mode). A parallel sequential scan uses multiple workers to scan a relation in parallel during a query in contrast to a serial sequential scan.
When set to
UNSAFE
, the function cannot be executed in parallel mode. The presence of such a function in a SQL statement forces a serial execution plan. This is the default setting if thePARALLEL
clause is omitted.When set to
RESTRICTED
, the function can be executed in parallel mode, but the execution is restricted to the parallel group leader. If the qualification for any particular relation has anything that is parallel restricted, that relation won't be chosen for parallelism.When set to
SAFE
, the function can be executed in parallel mode with no restriction.
COST execution_cost
execution_cost
is a positive number giving the estimated execution cost for the function, in units of cpu_operator_cost
. If the function returns a set, this is the cost per returned row. Larger values cause the planner to try to avoid evaluating the function more often than necessary.
ROWS result_rows
result_rows
is a positive number giving the estimated number of rows that the planner should expect the function to return. This is only allowed when the function is declared to return a set. The default assumption is 1000
rows.
SET configuration_parameter { TO value | = value | FROM CURRENT }
The SET
clause causes the specified configuration parameter to be set to the specified value when the function is entered, and then restored to its prior value when the function exits. SET FROM CURRENT
saves the session's current value of the parameter as the value to be applied when the function is entered.
If a SET
clause is attached to a function, then the effects of a SET LOCAL
command executed inside the function for the same variable are restricted to the function; the configuration parameter's prior value is restored at function exit. An ordinary SET
command (without LOCAL
) overrides the SET
clause, much as it would do for a previous SET LOCAL
command, with the effects of such a command persisting after procedure exit, unless the current transaction is rolled back.
PRAGMA AUTONOMOUS_TRANSACTION
PRAGMA AUTONOMOUS_TRANSACTION
is the directive that sets the function as an autonomous transaction.
Note
The STRICT
, LEAKPROOF
, PARALLEL
, COST
, ROWS
and SET
keywords provide extended functionality for Advanced Server and are not supported by Oracle.
Notes
Advanced Server allows function overloading; that is, the same name can be used for several different functions so long as they have distinct input (IN
, IN OUT
) argument data types.
Examples
The function emp_comp
takes two numbers as input and returns a computed value. The SELECT
command illustrates use of the function.
Function sal_range
returns a count of the number of employees whose salary falls in the specified range. The following anonymous block calls the function a number of times using the arguments’ default values for the first two calls.
The following example demonstrates using the AUTHID CURRENT_USER
clause and STRICT
keyword in a function declaration:
Include the STRICT
keyword to instruct the server to return NULL
if any input parameter passed is NULL
; if a NULL
value is passed, the function will not execute.
The dept_salaries
function executes with the privileges of the role that is calling the function. If the current user does not have sufficient privileges to perform the SELECT
statement querying the emp
table (to display employee salaries), the function will report an error. To instruct the server to use the privileges associated with the role that defined the function, replace the AUTHID CURRENT_USER
clause with the AUTHID DEFINER
clause.
Other Pragmas (declared within a package specification)
PRAGMA RESTRICT_REFERENCES
Advanced Server accepts but ignores syntax referencing PRAGMA RESTRICT_REFERENCES
.
See Also