0% found this document useful (0 votes)
150 views4 pages

Difference Between SP and UDF Is Listed Below

The document compares and contrasts three types of user-defined functions (UDFs) in SQL Server: scalar UDFs, inline table-valued UDFs, and multi-statement table-valued UDFs. Scalar UDFs return a single value, inline table-valued UDFs return a table directly from a SELECT statement, and multi-statement table-valued UDFs can contain multiple statements and return a user-defined table type. Examples are provided for each type of UDF.

Uploaded by

Sangameshpatil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
150 views4 pages

Difference Between SP and UDF Is Listed Below

The document compares and contrasts three types of user-defined functions (UDFs) in SQL Server: scalar UDFs, inline table-valued UDFs, and multi-statement table-valued UDFs. Scalar UDFs return a single value, inline table-valued UDFs return a table directly from a SELECT statement, and multi-statement table-valued UDFs can contain multiple statements and return a user-defined table type. Examples are provided for each type of UDF.

Uploaded by

Sangameshpatil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

The difference between SP and UDF is listed below:

User-Defined Functions
User-Defined Functions (UDFs) in Sql Server. All the examples in this article uses
the pubs database.
There are three Types of UDFS in Sql Server:
1. Scalar
2. Inline Table-Valued
3. Multi-statement Table-Valued
Let us go through each of these with examples:
1. Scalar User-Defined Function
A Scalar UDF can accept 0 to many input parameter and will return a single
value. A Scalar user-defined function returns one of the scalar (int, char,
varchar etc) data types. Text, ntext, image and timestamp data types are not
supported. These are the type of user-defined functions that most developers are
used to in other programming languages.

Example 1: Here we are creating a Scalar UDF AddTwoNumbers which accepts


two input parameters @a and @b and returns output as the sum of the two input
parameters.
1
CREATE FUNCTION AddTwoNumbers
2
(
3
@a int,
4
@b int
)
5
RETURNS int
6
AS
7
BEGIN
8
RETURN @a + @b
9
END
10
Once the above function is created we can use this function as below:
PRINT dbo.AddTwoNumbers(10,20)
--OR
SELECT dbo.AddTwoNumbers(30,20)

Note: For Scalar UDFS we need to use Two Part Naming Convention i.e. in the
above two statements we are using dbo.AddTwoNumbers.
Whether Below statement is correct? No, because it is not using two-part naming
convention. Try executing the below statement it will error out
PRINT AddTwoNumbers(10,20)

2. Inline Table-Valued User-Defined Function


An inline table-valued function returns a variable of data type table whose value
is derived from a single SELECT statement. Since the return value is derived
from the SELECT statement, there is no BEGIN/END block needed in the
CREATE FUNCTION statement. There is also no need to specify the table
variable name (or column definitions for the table variable) because the structure
of the returned value is generated from the columns that compose the SELECT
statement. Because the results are a function of the columns referenced in the
SELECT, no duplicate column names are allowed and all derived columns must
have an associated alias.

Example: In this example we are creating a Inline table-valued


functionGetAuthorsByState which accepts state as the input parameter and
returns firstname and lastname of all the authors belonging to the input state.
1
USE PUBS
2
GO
3
4
CREATE FUNCTION GetAuthorsByState
5
( @state char(2) )
6
RETURNS table
AS
7
RETURN (
8
SELECT au_fname, au_lname
9
FROM Authors
10
WHERE state=@state
11
)
GO
12
13
We can use the below statement to get all the authors in the state CA.
SELECT * FROM GetAuthorsByState('CA')

3. Multi-statement Table-Valued User-Defined Function


A Multi-Statement Table-Valued user-defined function returns a table. It can have
one or more than one T-Sql statement. Within the create function command you
must define the table structure that is being returned. After creating this type of
user-defined function, we can use it in the FROM clause of a T-SQL command
unlike the behavior found when using a stored procedure which can also return
record sets.
Example: In this example we are creating a Multi-Statement Table-Valued
functionGetAuthorsByState which accepts state as the input parameter and
returns author id and firstname of all the authors belonging to the input state. If
for the input state there are no authors then this UDF will return a record with no
au_id column value and firstname as No Authors Found.
1
2
3
4
5

USE PUBS
GO
CREATE FUNCTION GetAuthorsByState
( @state char(2) )
RETURNS

6
7
@AuthorsByState table (
8
au_id Varchar(11),
9
au_fname Varchar(20)
10
)
11
AS
BEGIN
12
13
INSERT INTO @AuthorsByState
14
SELECT au_id,
15
au_fname
16
FROM Authors
17
WHERE state = @state
18
19
IF @@ROWCOUNT = 0
BEGIN
20
INSERT INTO @AuthorsByState
21
VALUES ('','No Authors Found')
22
END
23
24
RETURN
25
END
GO
26
27
28
We can use the below statements to get all the authors in the given input state:
SELECT * FROM GetAuthorsByState('CA')
SELECT * FROM GetAuthorsByState('XY')

You might also like