A function is a Transact-SQL or common language runtime (CLR) routine that accepts parameters, performs an action, such as a complex calculation, and returns the result of that action as a value. The return value can either be a scalar (single) value or a table
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Function
1. A function is a Transact-SQL or common language runtime (CLR) routine
that accepts parameters, performs an action, such as a complex calculation,
and returns the result of that action as a value. The return value can either
be a scalar (single) value or a table.
The following is some important information about functions:
Functions only work with select statements.
Functions can be used anywhere in SQL, like AVG, COUNT, SUM, MIN,
DATE and so on with select statements.
Functions compile every time.
Functions must return a value or result.
Functions only work with input parameters.
Try and catch statements are not used in functions.
Types of Function:
There is basically two tyes of functions in SQL server:-
1. User Defined Function
These functions are created by user in system database or in user
defined database. We two types of user defined functions.
1. Table Valued Function.
2. Scalar Valued Function.
2. 1.1 Table Valued Function
In this type of function we select table data using a user created
function.
CREATE TABLE UserRegistration
(
C_Id int IDENTITY(1,1) NOT NULL,
C_Name varchar(100) NULL,
C_Age varchar(100) NULL,
C_Country varchar(100) NULL
);
Now Insert records in UserRegistration Table :-
Insert into UserRegistration(C_Name,C_Age,C_Country)
values('Ramesh','27','India')
Now we have to create one function like this:
create function GetUserRecords()
returns table
as return(select * from UserRegistration)
Now Call that function like this:
select * from GetUserRecords()
Note:This will return all records from table UserRegistration
3. 1.2 Scalar Valued Function:
User-defined scalar functions return a single data value of the type
defined in the RETURNS clause.
For an inline scalar function, there is no function body; the scalar value is
the result of a single statement.
create function GetUserAge
(
@name nvarchar(500)
)
returns table
As
return
(
select C_Age from UserRegistration where C_Name =
@name
)
Now call this function like this way
select * from GetUserAge('Ramesh')
For a multistatement scalar function, the function body, defined in a
BEGIN...END block, contains a series of Transact-SQL statements that
return the single value.
4. Create function MultistatementScalarFunction
(
@name varchar(100),
@Age varchar(100),
@Country varchar(100)
)
returns nvarchar(100)
As
Begin
Return
(
select (@name+'' +@Age+''+ @Country)
)
End
Now Call that function like this way:
select dbo.MultistatementScalarFunction(C_Name,C_Age,C_Country)as Info
from UserRegistration where C_Id=2
2.System Defined Function
These functionsaredefined by Sql Server for differentpurpose. Wehave
two types of system defined function in Sql Server
1. Scalar Function
2. Aggregate Function
5. 2.1 Scalar Function
Scalar functions operates on a single value and returns a single value.
Below is the list of some useful Sql Server Scalar functions.
2.1 Aggregate Function
Aggregate functions operates on a collection of values and returns a single
value. Below is the list of some useful Sql Server Aggregate functions.