Open In App

Multi-Statement Table Valued Function in SQL Server

Last Updated : 16 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In SQL Server, a multi-statement table-valued function (TVF) is a user-defined function that returns a table of rows and columns. Unlike a scalar function, which returns a single value, a TVF can return multiple rows and columns. 

Multi-statement  function is very much similar to inline functions only difference is that in multi-statement function we need to define the structure of the table  and also having the Begin and End block. 

Syntax:

CREATE FUNCTION function_name (@parameter_name data_type)

RETURNS @table_variable_name TABLE (column1 data_type, column2 data_type, …)

AS

BEGIN

   — function body (contains multiple statements)

   RETURN

END

Update/Alter Syntax:

ALTER FUNCTION function_name (@parameter_name data_type)

RETURNS @table_variable_name TABLE (column1 data_type, column2 data_type, …)

AS

BEGIN

   — function body (contains multiple statements)

   RETURN

END

DELETE Function Syntax:

DROP FUNCTION Function_Name

Now let’s see an example of a multi-statement Table Valued Function that returns a table of customers with order details :

Step 1: Create table Customer and Orders  :

--create table Customer
create table Customer (
customer_id int primary key,
contactname varchar(50) not null,
city varchar(20))

--Insert records
insert into Customer(customer_id,contactname,city)
values(1,'Maria Anders','Berlin'),
(2,'Ana Trujillo','London'),
(3,'Antonio Moreno','Lulea'),
(4,'Thomas Hardy','Madrid'),
(5,'Hanna Moos','London'),
(6,'Yang Wang','Bern')

select * from Customer

--create table orders
create table Orders(
order_id int primary key,
customer_id int,
order_date date,
country varchar(50))

--insert record
insert into Orders(order_id,customer_id,order_date,country) 
values(10252,4,'1996-07-09 00:00:00.000','Belgium'),
(10253,3,'1996-07-10 00:00:00.000','Brazil'),
(10254,5,'1996-07-11 00:00:00.000','Switzerland'),
(10255,9,'1996-07-12 00:00:00.000','Switzerland'),
(10256,3,'1996-07-15 00:00:00.000','Brazil'),
(10257,4,'1996-07-16 00:00:00.000','Austria'),
(10258,1,'1996-07-17 00:00:00.000','Austria')

select * from Orders

Output:

 

 

Step 2: Create a multi-statement function :

CREATE FUNCTION GetCustomersWithOrdersDetails ()
RETURNS @CustomersWithOrders TABLE 
(CustomerID int, ContactName nvarchar(50), 
Orderid int,orderdate date,city varchar(50))
AS
BEGIN
   INSERT INTO @CustomersWithOrders
   SELECT c.customer_id, c.ContactName, 
   order_id ,order_date,city
   FROM Customer c
   JOIN Orders o ON c.customer_id = o.customer_id
  
   RETURN
END

Output:

 

Step 3:Calliing the Function.

 

 

Here we can see above table gives the all information of customer from customer table and information about its orders from orders table.

So with the multi-statement table-valued function, we can get the output in a table format, and also with that function, we can use where ,order by , having clauses with aggregate functions . Simply we can say that this function create a table which gives us only the records or information that we want to show ,hence it useful for maintaining security.

Advantages :

The advantages of using Multi-Statement Table Valued Functions in SQL Server include:

  1. Reusability: By encapsulating complex logic and multiple SQL statements into a single function, Multi-Statement Table Valued Functions can be reused across multiple queries and applications. This can save time and effort by reducing the need to write and maintain redundant code.
  2. Improved Performance: By executing a single query that returns the necessary data, instead of multiple queries that each retrieve a subset of the data, Multi-Statement Table Valued Functions can improve query performance. They can also be optimized for specific scenarios, such as returning data in a particular order or retrieving data in a specific format.
  3. Data Transformation: With Multi-Statement Table Valued Functions, you can perform complex data transformations and combine data from multiple tables. This allows you to create custom views of your data that are tailored to your specific needs, without having to write a lot of complex SQL code.
  4. Maintainability: By encapsulating complex logic into a reusable function, Multi-Statement Table Valued Functions can make your code more maintainable and easier to debug. Changes to the logic can be made in a single location, which reduces the risk of introducing bugs or errors.
  5. Security: Multi-Statement Table Valued Functions can be used to restrict access to data, by allowing only specific users or roles to call the function. This can help to ensure that sensitive data is protected, while still allowing authorized users to access the information they need.

Disadvantages :

Some potential disadvantages to consider:

  1. Complexity: Multi-Statement Table Valued Functions can be more complex than simple SQL queries, and may require additional planning and design. This can make them harder to write, test, and maintain, especially for less experienced developers.
  2. Performance Issues: While Multi-Statement Table Valued Functions can improve query performance in some scenarios, they can also introduce performance issues if they are not optimized properly. This can result in slower query execution times and increased resource usage.
  3. Maintenance: Because Multi-Statement Table Valued Functions are a more complex construct than simple SQL queries, they can be harder to maintain over time. Changes to the underlying tables or queries may require updates to the function, which can introduce additional complexity and increase the risk of errors or bugs.
  4. Limited Compatibility: Multi-Statement Table Valued Functions may not be compatible with all versions of SQL Server, and may not be supported in other database platforms. This can limit their usefulness for developers who work with multiple platforms or need to maintain compatibility with older systems.

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads