create function (SQLJ)

Description

Creates a user-defined function by adding a SQL wrapper to a Java static method. Can return a value defined by the method.

Syntax

create function [owner.]sql_function_name
		( [ sql_parameter_name sql_datatype 
			[( length)| (precision[, scale ]) ]
		[ [, sql_parameter_name sql_datatype 
			[( length )| ( precision[, scale ]) ]]
		... ] ] )
	returns sql_datatype 
		[ ( length)| (precision[, scale ]) ]
	[modifies sql data] 
	[returns null on null input | 
		called on null input]
	[deterministic | not deterministic]
	[exportable]
	language java 
	parameter style java
	external name 'java_method_name 
		[ ( [java_datatype[, java_datatype 
		...] ] ) ] '

Parameters

sql_function_name

is the Transact-SQL name of the function. It must conform to the rules for identifiers and cannot be a variable.

sql_parameter_name

is the name of an argument to the function. The value of each input parameter is supplied when the function is executed. Parameters are optional; a SQLJ function need not take arguments.

Parameter names must conform to the rules for identifiers. If the value of a parameter contains non-alphanumeric characters, it must be enclosed in quotes. This includes object names qualified by a database name or owner name, since they include a period. If the value of the parameter begins with a numeric character, it also must be enclosed in quotes.

sql_datatype [(length) | ( precision [, scale])]

is the Transact-SQL datatype of the parameter. See create procedure for more information about these parameters.

sql_datatype is the SQL procedure signature.

returns sql_datatype

specifies the result datatype of the function.

modifies sql data

indicates that the Java method invokes SQL operations, reads, and modifies SQL data in the database. This is the default and only implementation. It is included for syntactic compatibility with the ANSI standard.

deterministic | not deterministic

included for syntactic compatibility with the ANSI standard. Not currently implemented.

exportable

specifies that the procedure is to be run on a remote server using the Adaptive Server OmniConnect™ feature. Both the procedure and the method it is built on must reside on the remote server.

language java

specifies that the external routine is written in Java. This is a required clause for SQLJ functions.

parameter style java

specifies that the parameters passed to the external routine at runtime are Java parameters. This is a required clause for SQLJ functions.

external

indicates that create function defines a SQL name for an external routine written in a programming language other than SQL.

name

specifies the name of the external routine (Java method). The specified name—java_method_name [ java_datatype[{, java_datatype} ...]]’—is a character-string literal and must be enclosed in single quotes.

java_method_name

specifies the name of the external Java method.

java_datatype

specifies a Java datatype that is mappable or result-set mappable. This is the Java method signature.

Examples

Example 1

This example creates a function square_root that invokes the java.lang.Math.sqrt() method:

create function square_root
	(input_number double precision) returns
		double precision
	language java parameter style java
	external name 'java.lang.Math.sqrt'

Usage

Permissions

Only the Database Owner or user with sa role can execute create function. The Database Owner or sa cannot transfer permission for create function.

See also

Commands create function (SQLJ), drop function (SQLJ)

System procedures sp_depends, sp_help, sp_helpjava, sp_helprotect