SQL
USER DEFINED FUNCTIONS
3 types of UDF
1. Scalar functions
2. Inline table valued function
3. Multi statement table valued function
1. Scalar functions
Scalar functions may or may not have parameters, but always return a single
(scalar) value.
The returned value can be of any data type, except text, ntext, image, cursor, and
timestamp.
EXAMPLE SYNTAX FOR CREATING SCALAR FUNCTION:
CREATE FUNCTION FUNCTION_NAME(@PARAMETER1 DATATYPE, @PARAMETER2 DATATYPE,
….., @PARAMETERN DATATYPE)
RETURNS RETURN_DATATYPE
AS
BEGIN
FUNCTION_BODY
RETURN return_datatype
END
Example code + output
CREATE FUNCTION CALCULATE_AGE(@DOB DATE)
RETURNS INT
AS
BEGIN
DECLARE @AGE INT
SET @AGE=DATEDIFF(YEAR,@DOB,GETDATE())-
CASE WHEN (MONTH(@DOB)>MONTH(GETDATE())) OR
MONTH(@DOB)=MONTH(GETDATE()) AND DAY(@DOB)>DAY(GETDATE())
THEN 1
ELSE 0
END
RETURN @AGE
END
SELECT STATEMENT
SELECT dbo.CALCULATE_AGE ('01/04/2001') as Age
OUTPUT
Code :
select id, name , dbo.CALCULATE_AGE1(DOB) as AGE from
Employees;
OUTPUT:
A stored procedure also can accept DOB and return Age, but you cannot use stored
procedures in a select or where clause. This is just one difference between a function
and a stored procedure.
To,
alter a function we use ALTER FUNCTION FunctionName statement and to
delete it, we use DROP FUNCTION FunctionName.
INLINE TABLE VALUED FUNCTIONS
In, SCALAR FUNCTION – RETURNS A SCALAR VALUE
but in,
INLINE TABLE VALUED FUNCTION – RETURNS A TABLE
Key points about Inline Table-Valued Functions in SQL:
✓ We specify TABLE as the return type, instead of any scalar data type.
✓ The function body is not enclosed between BEGIN and END block.
✓ The structure of the table that gets returned is determined by the SELECT
statement within the function.
We can use Inline table valued functions to :-
• Achieve the functionality of parameterized views .
• Table returned by the table valued function, can also be used with joins in the
other table.
Temporary Table
Temporary tables are similar to tables, tables created and stored in the database until
you delete or drop the table. Whereas temporary tables are stored in TempDB, it will
vanish if it is no longer in use.
Different types of Temporary tables:
1. Local Temporary table
2. Global Temporary table
To create a temporary table, we will use # to define the table name.
1. Local Temporary Table:
Temporary tables are stored in TempDB and the name of the DB is suffixed with a lot of
underscores and random numbers.
Local temporary tables are available only for the connection that has created the table
and automatically dropped if the connection is closed.
2. Global Temporary Table:
Global Temporary tables are visible to all the connections of the SQL Server and are
only destroyed when the last connection referencing the table is closed.
The name should be unique, because it is accessible from other connections.
To create a Global temporary table, give two pound symbols (##) as the prefix of the
table name.
Comparing Local vs Global Temporary Tables in SQL Server
Local Temporary Table Global Temporary Table
Feature
(#TableName) (##TableName)
Uses a single # before the table Uses a double ## before the table
Table Name Prefix
name name
Accessible only to the session
Accessible to all active sessions
Visibility (or connection) that created it
on the server while it exists
(including nested procedures)
Automatically removed when
Removed only after the session
the session ends or a
When Dropped that created it ends and no other
procedure completes (if
sessions are using it
created inside one)
SQL Server appends a unique
Table name must be globally
internal identifier, allowing
unique. Creating ##SameTable
Naming Rules multiple sessions to create the
again results in error (e.g., "object
same table name like
already exists")
#TempTable without conflict
Allows shared temporary data
Suitable for isolated, session-
across sessions, but not
Use Case Scope specific tasks or within stored
recommended due to complexity
procedures
and possible conflicts