CREATE FUNCTION Statement

Creates a user-defined function in the database. A function can be created for another user by specifying an owner name. Subject to permissions, a user-defined function can be used in exactly the same way as other non-aggregate functions.

Syntax

Syntax 1

CREATE [ OR REPLACE ] [ TEMPORARY ] FUNCTIONowner.]function-name  ( [ parameter, … ] )
   [ SQL SECURITYINVOKER | DEFINER } ]
   RETURNS data-type ON EXCEPTION RESUME 
   | [ NOT ] DETERMINISTICcompound-statementAS tsql-compound-statement
   | EXTERNAL NAME library-call 
   | EXTERNAL NAME java-call LANGUAGE JAVA }

parameter:
   IN parameter-name data-type DEFAULT expression ]

routine-characteristics:
   ON EXCEPTION RESUME | [ NOT ] DETERMINISTIC

tsql-compound-statement:
   sql-statement 
    sql-statement

library-call:
   '[ operating-system:]function-name@library; …'

operating-system:
   UNIX

java-call:
   '[ package-name.]class-name.method-name method-signature'

method-signature:
   ( [ field-descriptor, ….] ) return-descriptor

field-descriptor and return-descriptor:
   Z | B | S | I | J | F | D | C | V | descriptor | L class-name;

Syntax 2

CREATE FUNCTIONowner.]function-name ( [ parameter, … ] )
   RETURNS data-type
   URL url-string
   [ HEADER header-string ]
   [ SOAPHEADER soap-header-string ]
   [ TYPE { 'HTTP[:{ GET | POST } ] ' | 'SOAP[:{ RPC | DOC } ]' } ]
   [ NAMESPACE namespace-string ]
   [ CERTIFICATE certificate-string ]
   [ CLIENTPORT clientport-string ]
   [ PROXY proxy-string ]

url-string:
   ' { HTTP | HTTPS | HTTPS_FIPS }://[user:password@]hostname[:port][/path] '

parameter:
   IN parameter-name data-type DEFAULT expression ]

Parameters

Examples

Usage

To modify a user-defined function, or to hide the contents of a function by scrambling its definition, use the ALTER FUNCTION statement.

When functions are executed, not all parameters need to be specified. If a default value is provided in the CREATE FUNCTION statement, missing parameters are assigned the default values. If an argument is not provided by the caller and no default is set, an error is given.

Side Effects
  • Automatic commit

Standards

  • SQL—ISO/ANSI SQL compliant.

  • Sybase—Not supported by Adaptive Server Enterprise.

Permissions

For function to be owned by self – Requires the CREATE PROCEDURE system privilege.

For function to be owned by any user – Requires one of:
  • CREATE ANY PROCEDURE system privilege.
  • CREATE ANY OBJECT system privilege.
To create a function containing an external reference, regardless of whether or not they are the owner of the function, also requires the CREATE EXTERNAL REFERENCE system privilege.