create function

Creates a user-defined function, which is a saved Transact-SQL routine that returns a specified value.

Syntax

create [or replace] function [ owner_name. ] function_name 
	[ ( @parameter_name [as] parameter_datatype [ = default ]
		[ ,...n ] ) ] 
	returns return_datatype
	[ with recompile]
	as 
	[begin]
	function_body 
	return scalar_expression
	[end]

Parameters

Examples

Usage

  • If the owner of the user-defined function also owns all the database objects referenced inside, then all the other users who have execute permission on the function are automatically granted access permissions to all the referenced objects when they execute the function.

  • When a function is created, the SAP ASE server checks to see if it is a SQL user-defined function or a SQLJ user-defined function. If it is the latter, the SAP ASE server checks for “sa” permissions. If it is a SQL function, the SAP ASE server checks for create function privileges.

If a function is referenced in a computed column or functional index, it cannot be replaced.

Permissions

Any user who impersonates the function owner through an alias or setuser cannot replace the function.

The permission checks for create function differ based on your granular permissions settings.

SettingDescription
Enabled

With granular permissions enabled, you must have the create function privilege to create a function. To create a function for another user, you must have the create any function privilege.

You must be the function owner to replace the function.

Disabled

With granular permissions disabled, you must be the database owner, a user with sa_role, or have the create function privilege to create a function. To create a function for another user, you must have sa_role.

You must be the function owner to replace the function.

Auditing

Values in event and extrainfo columns of sysaudits are:

InformationValues
Event

97

Audit option

create

Command or access audited

create function

Information in extrainfo
  • Roles – current active roles

  • Keywords or options – NULL

  • Previous value – NULL

  • Current value – NULL

  • Other information – NULL

  • Proxy information – original login name, if set proxy is in effect

  • or replace – for create or replace