What is a function in SQL Server?
In SQL Server, a function is a stored program that you can pass parameters into and return a value.
Create Function
You can create your own functions in SQL Server (Transact-SQL). Let's take a closer look.
Syntax
The syntax to create a function in SQL Server (Transact-SQL) is:
CREATE FUNCTION [schema_name.]function_name
( [ @parameter [ AS ] [type_schema_name.] datatype
[ = default ] [ READONLY ]
, @parameter [ AS ] [type_schema_name.] datatype
[ = default ] [ READONLY ] ]
)
RETURNS return_datatype
[ WITH { ENCRYPTION
| SCHEMABINDING
| RETURNS NULL ON NULL INPUT
| CALLED ON NULL INPUT
| EXECUTE AS Clause ]
[ AS ]
BEGIN
[declaration_section]
executable_section
RETURN return_value
END;
- schema_name
- The name of the schema that owns the function.
- function_name
- The name to assign to this function in SQL Server.
- @parameter
- One or more parameters passed into the function.
- type_schema_name
- The schema that owns the data type, if applicable.
- datatype
- The data type for @parameter.
- default
- The default value to assign to @parameter.
- READONLY
- It means that @parameter can not be overwritten by the function.
- return_datatype
- The datatype of the function's return value.
- ENCRYPTION
- It means that the source for the function will not be stored as plain text in the system views in SQL Server.
- SCHEMABINDING
- It means that the underlying objects can not be modified so as to affect the function.
- RETURNS NULL ON NULL INPUT
- It means that the function will return NULL if any parameters are NULL without having to execute the function.
- CALL ON NULL INPUT
- It means that the function will execute the function even if any parameters are NULL.
- EXECUTE AS clause
- Sets the security context to execute the function.
- return_value
- The value returned by the function.
Drop Function
Once you have created your function in SQL Server (Transact-SQL), you might find that you need to remove it from the database.Syntax
The syntax to a drop a function in SQL Server (Transact-SQL) is:DROP FUNCTION function_name;
- function_name
- The name of the function that you wish to drop.
Comments
Post a Comment