SQL Server now supports table valued parameters which allow us to send data tables as parameters to Stored Procedures. It still uses the same ADO.NET API.
Description
Table Valued Parameter allows a table (i.e multiple rows of data) to be passed as a parameter to a stored procedure from T-SQL code or from an application. It was not possible to pass a table variable as a parameter to a stored procedure in old versions of SQL Server.
To pass multiple rows to a stored procedure using Table Valued Parameter, follow the below steps.
Steps
Create one table named "MyUDTable".
- Create Table MyUDTable
- (
- Id int primary key,
- Name nvarchar(50),
- Gender nvarchar(10)
- )
- Go
- CREATE TYPE MyUDTableType AS TABLE
- (
- Id INT PRIMARY KEY,
- Name NVARCHAR(50),
- Gender NVARCHAR(10)
- )
- Go

Create one stored procedure named "Sp_InsertMyUDTableByMyUDTableType".
- CREATE PROCEDURE Sp_InsertMyUDTableByMyUDTableType
- @MyUDTableType MyUDTableType READONLY
- AS
- BEGIN
- INSERT INTO MyUDTable
- SELECT * FROM @MyUDTableType
- END
Let's use the user-defined TableType as a parameter in the stored procedure.
Table valued parameters must be passed as read-only to stored procedures, functions etc. This means you are unable to perform DML operations like INSERT, UPDATE or DELETE on a table-valued parameter in the body of a function, stored procedure etc.
Then, run the below SQL
- select * from MyUDTable

Declare a table variable, insert the data and then pass the table variable as a parameter to the stored procedure.
- DECLARE @MyUserDTableType MyUDTableType
- INSERT INTO @MyUserDTableType VALUES (1, 'Mark', 'Male')
- INSERT INTO @MyUserDTableType VALUES (2, 'Mary', 'Female')
- INSERT INTO @MyUserDTableType VALUES (3, 'John', 'Male')
- INSERT INTO @MyUserDTableType VALUES (4, 'Sara', 'Female')
- EXECUTE Sp_InsertMyUDTableByMyUDTableType @MyUserDTableType
In insert statement we used table variable instead of Columns of tabled named "MyUDTable".
Then execute stored procedure to insert records to table named "MyUDTable" by using table variable named "@MyUserDTableType".
Then run the below sql
Then execute stored procedure to insert records to table named "MyUDTable" by using table variable named "@MyUserDTableType".
Then run the below sql
- select * from MyUDTable

All 4 records are inserted successfully in the tabled named "MyUDTable".
Merits
Code size in C# will be reduced using ony single user-defined TableType parameter instead of many parameters of stored procedure. For example,
Without User-defined Table Type as a parameter:
To execute this procedure, we can create a data table and add the rows into it. Then, pass this data table as a parameter to the database.
Merits
Code size in C# will be reduced using ony single user-defined TableType parameter instead of many parameters of stored procedure. For example,
Without User-defined Table Type as a parameter:
To execute this procedure, we can create a data table and add the rows into it. Then, pass this data table as a parameter to the database.
- DataTable dt = new DataTable();
- using(SqlConnection conn = new SqlConnection("your connection string"))
- {
- SqlCommand cmd = conn.CreateCommand();
- cmd.CommandType = System.Data.CommandType.StoredProcedure;
- cmd.CommandText = "dbo.Sp_InsertSampleDetails"; //The name of the other stored procedure.
- SqlParameter param = cmd.Parameters.AddWithValue("@param1", dt); //The next 4 related parameters of this other stored procedure.
- SqlParameter param = cmd.Parameters.AddWithValue("@param2", dt);
- SqlParameter param = cmd.Parameters.AddWithValue("@param3", dt);
- SqlParameter param = cmd.Parameters.AddWithValue("@param4", dt);
- conn.Open();
- cmd.ExecuteNonQuery();
- conn.Close();
- }
Here, I put all the parameters with stored procedure with 4 lines of code.
with User-defined TableType as a parameter,
with User-defined TableType as a parameter,
- using(SqlConnection conn = new SqlConnection("your connection string"))
- {
- SqlCommand cmd = conn.CreateCommand();
- cmd.CommandType = System.Data.CommandType.StoredProcedure;
- cmd.CommandText = "dbo.Sp_InsertMyUDTableByMyUDTableType"; //The name of the above mentioned stored procedure.
- SqlParameter param = cmd.Parameters.AddWithValue("@MyUDTableType", dt); //Here"@MyUDTableType" is the User-defined Table Type as a parameter.
- conn.Open();
- cmd.ExecuteNonQuery();
- conn.Close();
- }
Summary
In this blog, we have learned the following.
- What is User-Defined Table Type.
- How to implement it in stored procedure.
- How to implement it in C# Code-Behind File.
- Merits of using User-Defined TableType in real time.

Rajan KumarPosted Apr 27, 2022, 12:29 PM
Nice Articles sir jee
Sonam GalaniPosted Jan 19, 2018, 1:50 AM
What is the other stored procedure that do not use user defined table type?
Zeeshan AzimPosted Oct 11, 2017, 12:04 AM
Good work. Amazin technique
Saravanakumar SekaranPosted May 10, 2017, 3:19 AM
Wow nice one !!!!!!!!!