Creates a user-defined function, which is a saved Transact-SQL routine that returns a specified value.
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]
When used with or replace, is the name of the function remains the same, although its definition is changed.
Specify a parameter name using an “at” sign (@) as the first character. The parameter name must conform to the rules for identifiers. Parameters are local to the function. You can use the same parameter names in other functions.
If a parameter has a default value, the user must specify the keyword “default” when they call the function to get the default value.
When used with or replace, you can alter the names and number of parameters.
When used with or replace, you can change the datatype of the parameter to the function.
When used with or replace, you can change the option to recompile, or not to recompile every time the function is replaced.
When used with or replace, you can alter the return datatype of the function.
You can invoke scalar-valued functions where scalar expressions are used, including computed columns and check constraint definitions.
When used with or replace, you can change the value returned by the function.
When used with or replace, you can change the SQL statements that define the value of the function
create function BONUS(@salary int, @grade int, @dept_id int) returns int as begin declare @bonus int declare @cat int set @bonus = 0 select @cat = dept_cat from department where dept_id = @dept_id if (@cat < 10) begin set @bonus = @salary *15/100 end else begin set @bonus = @salary * 10/100 end return @bonus end
create function fullname( @firstname char(30), @lastname char(30)) returns char(61) as begin declare @name char(61) set @name = @firstname|| ‘ ‘ ||@lastname return @name end select object_id("fullname") ----------- 473049690
create or replace function fullname( @firstname char(30), @lastname char(30)) returns char(61) as begin return(@firstname|| ' ' ||@lastname) end select object_id("fullname") ----------- 473049690
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.
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.
Setting | Description |
---|---|
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. |
Values in event and extrainfo columns of sysaudits are:
Information | Values |
---|---|
Event | 97 |
Audit option | create |
Command or access audited | create function |
Information in extrainfo |
|