Lect 3
Lect 3
Lect 3
Advanced Database II
Lecture 3
SQL Server Functions
• User-Defined Functions
• Advantages of UDFs
• Types of User-Defined Functions
• Creation and Execution of User-Defined Functions
▪ We can invoke a UDF by the code, just like you’d call a system function.
▪ For example, you could use a UDF in a WHERE clause in order to narrow the
results of a SELECT statement.
▪ scalar functions can be called using the EXECUTE statement, the same way
as stored procedures.
END
Calling the created function
Let to calculate the total price of each product in the Products table, and the result is displayed
in a column named Total.
Step 1: Create Function
▪ This type of function is special because it returns a table that you can query the
results of a join with other tables.
▪ A Table Valued function is further categorized into an “Inline Table Valued Function”
and a “Multi-Statement Table Valued Function”.
Once you create the above function, then call it like below:
▪ In the function body you can read data from databases and do some operations.
▪ We can define this function by using a table variable as the return value. Inside the
function, we execute multiple queries and insert data into this table variable.
Multi-Statement Table-Valued
Function example