----------------------------
How can we write user-defined functions (UDF) in my SQL server?
There are three types of user-defined functions(UDF) in SQL server
1)Scalar function
2)Inline function
3)Multi statement Table-valued function
1)Scalar function:-
->Scalar UDFs return a single value.
->Scalar UDFs built-in functions such as GETDATE(),or OBJECT_NAME(), which returns single value,date or integer. The value returned by a Scalar UDF can be based on the parameters passed
Syntax:-
Create Functions function-name(Parameters)
Returns returns-type
AS
BEGIN
Statements 1
Statements 2
Statements 3
.
.
Statements n
END
Example:-
Create Functions GetEmployee(@DepartmentID int)
Returns varchar(50)
AS
BEGIN
RETURN (SELECT Name FROM Employee WHERE DepartmentID=@DepartmentID )
END
2)Inline function:-
->Inline UDFs return single rows or multiple rows and can contain a single SELECT statement. Because in-line UDF is limited to a single SELECT statement.
Syntax:-
Create Functions function-name(Parameters)
Returns returns-type
AS
BEGIN
RETURN
Example:-
Create Functions GetEmployee(@DepartmentID int)
Returns varchar(50)
AS
BEGIN
RETURN
SELECT Name FROM Employee WHERE DepartmentID=@DepartmentID
3)Multi statement Table-valued function:-
->Multi-statements UDF can contain any number of statements that populated the table variable to be returned.
Syntax:-
Create Functions function-name(Parameters)
Returns @TableName TABLE
(
Column_1 Datatype,
Column_2 Datatype,
.
Column_n Datatype
)
AS
BEGIN
Statements 1
Statements 2
Statements 3
.
.
Statements n
RETURN
END
Example:-
Create Functions GETaverage(@Name varchar(50))
Returns @Marks TABLE
(
Name varchar(50),
Class varchar(50),
Average Decimal(4,2)
)
AS
BEGIN
DECLARE @Avg DECIMAL(4,2)
DECLARE @Rno INT
INSERT INTO @Marks (Name)VALUES(@Name)
SELECT @Rno=Rno FROM Student WHERE Name=@Name
SELECT @Avg=(Subject1+Subject2+Subject3)/3 FROM Subjects WHERE Rno=@Rno
UPDATE @Marks SET
Subject1=(SELECT Subject1 FROM Subjects WHERE Rno=@Rno),
Subject2=(SELECT Subject2 FROM Subjects WHERE Rno=@Rno),
Subject3=(SELECT Subject3 FROM Subjects WHERE Rno=@Rno)
Average=@Avg
WHERE Name=@Name
RETURN
END
How can we write user-defined functions (UDF) in my SQL server?
There are three types of user-defined functions(UDF) in SQL server
1)Scalar function
2)Inline function
3)Multi statement Table-valued function
1)Scalar function:-
->Scalar UDFs return a single value.
->Scalar UDFs built-in functions such as GETDATE(),or OBJECT_NAME(), which returns single value,date or integer. The value returned by a Scalar UDF can be based on the parameters passed
Syntax:-
Create Functions function-name(Parameters)
Returns returns-type
AS
BEGIN
Statements 1
Statements 2
Statements 3
.
.
Statements n
END
Example:-
Create Functions GetEmployee(@DepartmentID int)
Returns varchar(50)
AS
BEGIN
RETURN (SELECT Name FROM Employee WHERE DepartmentID=@DepartmentID )
END
2)Inline function:-
->Inline UDFs return single rows or multiple rows and can contain a single SELECT statement. Because in-line UDF is limited to a single SELECT statement.
Syntax:-
Create Functions function-name(Parameters)
Returns returns-type
AS
BEGIN
RETURN
Example:-
Create Functions GetEmployee(@DepartmentID int)
Returns varchar(50)
AS
BEGIN
RETURN
SELECT Name FROM Employee WHERE DepartmentID=@DepartmentID
3)Multi statement Table-valued function:-
->Multi-statements UDF can contain any number of statements that populated the table variable to be returned.
Syntax:-
Create Functions function-name(Parameters)
Returns @TableName TABLE
(
Column_1 Datatype,
Column_2 Datatype,
.
Column_n Datatype
)
AS
BEGIN
Statements 1
Statements 2
Statements 3
.
.
Statements n
RETURN
END
Example:-
Create Functions GETaverage(@Name varchar(50))
Returns @Marks TABLE
(
Name varchar(50),
Class varchar(50),
Average Decimal(4,2)
)
AS
BEGIN
DECLARE @Avg DECIMAL(4,2)
DECLARE @Rno INT
INSERT INTO @Marks (Name)VALUES(@Name)
SELECT @Rno=Rno FROM Student WHERE Name=@Name
SELECT @Avg=(Subject1+Subject2+Subject3)/3 FROM Subjects WHERE Rno=@Rno
UPDATE @Marks SET
Subject1=(SELECT Subject1 FROM Subjects WHERE Rno=@Rno),
Subject2=(SELECT Subject2 FROM Subjects WHERE Rno=@Rno),
Subject3=(SELECT Subject3 FROM Subjects WHERE Rno=@Rno)
Average=@Avg
WHERE Name=@Name
RETURN
END