FUNCTION (Function) in SQL Server
The function in SQL Server is stored in the database so you can pass the parameters as well as return the values. The article will give you the syntax and examples of how to create and delete functions in SQL Server.
Function (function) is an object in the database that consists of a set of multiple statements grouped together and created for reuse purposes. In SQL Server, functions are stored and you can pass in parameters as well as return values.
The article will give you the syntax and examples of how to create and delete functions in SQL Server.
CREATE FUNCTION (Create Function)
Syntax
To create a function in SQL Server, we use the following syntax:
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;
Parameters:
- schema_name: schema name (schema) with function.
- function_name: Name assigned to the function.
- @parameter: One or more parameters are passed into the function.
- type_schema_name: Data type of schema (if any).
- Datatype: Data type for @parameter.
- Default: The default value assigned to @parameter.
- READONLY: @parameter cannot be overwritten by the function.
- return_datatype: Data type of return value.
- ENCRYPTION: The source code of the function will not be stored as text in the system.
- SCHEMABINDING: Make sure objects that are not edited affect the function.
- RETURNS NULL ON NULL INPUT : The function will return NULL if any parameter is NULL.
- CALL ON NULL INPUT : The function will execute even if the parameter is NULL.
- EXECUTE AS clause: Specifies the security context to execute the function.
- return_value: Value returned.
For example
CREATE FUNCTION fuNhanvien
(@nhanvien_id INT)
RETURNS VARCHAR (50)
AS
BEGIN
DECLARE @nhanvien_name VARCHAR (50);
IF @nhanvien_id <10
SET @nhanvien_name = 'Smith';
ELSE
SET @nhanvien_name = 'Lawrence';
RETURN @nhanvien_name;
END;
The above function is named fuNhanvien, has an @nhanvien_id parameter with the INT data type. The function will return the VARCHAR (50) value when executing the RETURNS statement.
Then you can make the reference fuNhanvien as follows:
USE [test]
GO
SELECT dbo.fuNhanvien (8);
GO
Drop Function (Delete Function)
Once you have created the function successfully, there will be cases where you want to remove the function from the database for a few reasons.
Syntax
To remove a function, we have the following syntax:
DROP FUNCTION function_name ;
Parameters :
function_name: The function name you want to delete .
For example
DROP FUNCTION fuNhanvien;
By executing this command, you have just removed the fu function from the database.
Previous post: SEQUENCE in SQL Server
Next lesson: PROCEDURE (Procedure) in SQL Server
You should read it
- DAY function in SQL Server
- MIN function in SQL Server
- MAX function in SQL Server
- SUM function in SQL Server
- AVG function in SQL Server
- ABS function in SQL Server
- DATEPART function in SQL Server
- DATENAME function in SQL Server
- RIGHT function in SQL Server
- ROUND function in SQL Server
- CEILING function in SQL Server
- FLOOR function in SQL Server
Maybe you are interested
We may be able to see the 'second moon' in the sky with the naked eye next May The most bizarre things people ever bring to space Will the Earth be affected if the comet once destroyed the dinosaurs hitting the Sun? 'Wow!' - Has the mysterious cosmic signal of 'alien' been decoded? True moon, snow moon and comet will appear together today The 7 largest objects people ever launched into space