Use this statement to create a new function in the database.
CREATE FUNCTION [ owner.]function-name ( [ parameter, ... ] )
RETURNS data-type routine-characteristics
{ compound-statement
| AS tsql-compound-statement
| external-name }
CREATE FUNCTION [ owner.]procedure-name ( [ parameter, ... ] )
RETURNS data-type
URL url-string
[ TYPE { 'HTTP[:{GET|POST}]' | 'SOAP[:{RPC|DOC}]' } ]
[ NAMESPACE namespace-string ]
[ CERTIFICATE certificate-string ]
[ CLIENTPORT clientport-string ]
[ PROXY proxy-string ]
url-string :
'{HTTP|HTTPS}://[user:password@]hostname[:port][/path]'
parameter :
[ IN ] parameter-name data-type
routine-characteristics
ON EXCEPTION RESUME | [ NOT ] DETERMINISTIC
tsql-compound-statement:
sql-statement
sql-statement
...
external-name:
EXTERNAL NAME library-call
| EXTERNAL NAME java-call LANGUAGE JAVA
library-call :
'[operating-system:]function-name@library; ...'
operating-system :
Windows95 | WindowsNT | NetWare | UNIX
java-call :
'[package-name.]class-name.method-name method-signature'
method-signature :
([field-descriptor, ... ]) return-descriptor
field-descriptor | return-descriptor :
Z | B | S | I | J | F | D | C | V | [descriptor | Lclass-name;
CREATE FUNCTION clause Parameter names must conform to the rules for database identifiers. They must have a valid SQL data type, and must be prefixed by the keyword IN, signifying that the argument is an expression that provides a value to the function.
compound-statement A set of SQL statements bracketed by BEGIN and END, and separated by semicolons. See BEGIN statement
tsql-compound-statement A batch of Transact-SQL statements. See Transact-SQL batch overview, and CREATE PROCEDURE statement [T-SQL].
EXTERNAL NAME clause A function using the EXTERNAL NAME clause is a wrapper around a call to a function in an external library. A function using EXTERNAL NAME can have no other clauses following the RETURNS clause. The library name may include the file extension, which is typically .dll on Windows, .so on UNIX, and .nlm on NetWare. In the absence of the extension, the software appends the platform-specific default file extension for libraries. On NetWare, if no NLM name is given, the NLM containing the symbol must already be loaded when the function is called.
For information about external library calls, see Calling external libraries from procedures.
EXTERNAL NAME LANGUAGE JAVA clause A function that uses EXTERNAL NAME with a LANGUAGE JAVA clause is a wrapper around a Java method.
For information on calling Java procedures, see CREATE PROCEDURE statement.
ON EXCEPTION RESUME clause Use Transact-SQL -like error handling. For more information, see CREATE PROCEDURE statement.
NOT DETERMINISTIC clause A function specified as NOT DETERMINISTIC is re-evaluated each time it is called in a query. The results of functions not specified in this manner may be cached for better performance, and re-used each time the function is called with the same parameters during query evaluation.
Functions that have side effects such as modifying the underlying data should be declared as NOT DETERMINISTIC. For example, a function that generates primary key values and is used in an INSERT ... SELECT statement should be declared NOT DETERMINISTIC:
CREATE FUNCTION keygen( increment INTEGER ) RETURNS INTEGER NOT DETERMINISTIC BEGIN DECLARE keyval INTEGER; UPDATE counter SET x = x + increment; SELECT counter.x INTO keyval FROM counter; RETURN keyval END INSERT INTO new_table SELECT keygen(1), ... FROM old_table
Functions may be declared as DETERMINISTIC if they always return the same value for given input parameters. Future versions of the software may use this declaration to allow optimizations that are unsafe for functions that could return different values for the same input.
URL clause For use only when defining a HTTP or SOAP web services client function. Specifies the URI of the web service. The optional username and password parameters provide a means of supplying the credentials needed for HTTP Basic Authentication. HTTP Basic Authentication base-64 encodes the user and password information and passes it in the "Authentication" header of the HTTP request.
TYPE clause Used to specify the format used when making the web service request. If SOAP is specified or no type clause is included, the default type SOAP:RPC is used. HTTP implies HTTP:POST. Since SOAP requests are always sent as XML documents, HTTP:POST is always used to send SOAP requests.
NAMESPACE clause Applies to SOAP client functions only. This clause identifies the method namespace usually required for both SOAP:RPC and SOAP:DOC requests. The SOAP server handling the request uses this namespace to interpret the names of the entities in the SOAP request message body. The namespace can be obtained from the WSDL description of the SOAP service available from the web service server. The default value is the procedure's URL, up to but not including the optional path component.
CERTIFICATE clause In order to make a secure (HTTPS) request, a client must have access to the certificate used by the HTTPS server. The necessary information is specified in a string of semicolon-separated key/value pairs. The certificate may be placed in a file and the name of the file provided using the file key, or the whole certificate may be placed in a string, but not both. The following keys are available:
| Key | Abbreviation | Description |
|---|---|---|
| file | The filename of the certificate. | |
| certificate | cert | The certificate itself. |
| company | co | The company specified in the certificate. |
| unit | The company unit specified in the certificate. | |
| name | The common name specified in the certificate. |
Certificates are required only for requests that are either direced to an HTTPS server, or may be redirected from a non-secure to a secure server. A certificate value is not required when sent to an Adpative Server Anywhere database server that accepts HTTPS requests, unless a certificate parameter was used when the database server was started.
CLIENTPORT clause Identifies the port number on which the HTTP client procedure communicates using TCP/IP. It is provided for and recommended only for connections across firewalls, as firewalls filter according to the TCP/UDP port. You may specify a single port numbers, ranges of port numbers, or a combination of both; for example, CLIENTPORT '85,90-97'.
For more information, see ClientPort protocol option [CPORT].
PROXY clause Specifies the URI of a proxy server. For use when the client must access the network through a proxy. Indicates that the procedure is to connect to the proxy server and send the request to the web service through it.
The 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.
Adaptive Server Anywhere treats all user-defined functions as idempotent unless they are declared NOT DETERMINISTIC. Idempotent functions return a consistent result for the same parameters and are free of side effects. That is, the server assumes that two successive calls to the same function with the same parameters will return the same result, and will not have any unwanted side-effects on the query's semantics.
If a function returns a result set, it cannot also set output parameters or return a return value.
For web service client functions, the return type of SOAP and HTTP functions must one of the character data types, such as VARCHAR. The value returned is the body of the HTTP response. No HTTP header information is included. If more information is required, such as status information, use a procedure instead of a function.
Parameter values are passed as part of the request. The syntax used depends on the type of request. For HTTP:GET, the paramters are passed as part of the URL; for HTTP:POST requests, the values are placed in the body of the request. Parameters to SOAP requests are always bundled in the request body.
Must have RESOURCE authority.
External functions, including Java functions, must have DBA authority.
Automatic commit.
SQL/92 Persistent Stored Module feature.
SQL/99 Persistent Stored Module feature.
Sybase Not supported by Adaptive Server Enterprise. The CREATE FUNCTION statement is supported by Microsoft SQL Server.
The following function concatenates a firstname string and a lastname string.
CREATE FUNCTION fullname ( firstname CHAR(30), lastname CHAR(30) ) RETURNS CHAR(61) BEGIN DECLARE name CHAR(61); SET name = firstname || ' ' || lastname; RETURN (name); END
The following examples illustrate the use of the fullname function.
Return a full name from two supplied strings:
SELECT fullname ('joe','smith')| fullname('joe', 'smith') |
|---|
| joe smith |
List the names of all employees:
SELECT fullname (emp_fname, emp_lname) FROM employee
| fullname (emp_fname, emp_lname) |
|---|
| Fran Whitney |
| Matthew Cobb |
| Philip Chin |
| Julie Jordan |
| ... |
The following function uses Transact-SQL syntax:
CREATE FUNCTION DoubleIt ( @Input INT ) RETURNS INT AS DECLARE @Result INT SELECT @Result = @Input * 2 RETURN @Result
The statement SELECT DoubleIt( 5 ) returns a value of 10.
The following statement creates an external function written in Java:
CREATE FUNCTION dba.encrypt( IN name char(254) ) RETURNS VARCHAR EXTERNAL NAME 'Scramble.encrypt (Ljava/lang/String;)Ljava/lang/String;' LANGUAGE JAVA
SQL Anywhere Studio 9.0.2
Copyright © 1989–2004 Sybase, Inc. Portions copyright © 2001–2004 iAnywhere Solutions, Inc. All rights reserved.