Table valued parameters in SQL Server

Table valued parameters in SQL Server

table valued parameters example
send table variable to stored procedure
pass table variable as parameter to stored procedure
pass table variable to sql stored procedure

In this video we will discuss table valued parameters in SQL Server.

Table Valued Parameter is a new feature introduced in SQL SERVER 2008. 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. Prior to SQL SERVER 2008, it is not possible to pass a table variable as a parameter to a stored procedure.

Let us understand how to pass multiple rows to a stored procedure using Table Valued Parameter with an example. We want to insert multiple rows into the following Employees table.

SQL Script to create the Employees table
Create Table Employees
(
Id int primary key,
Name nvarchar(50),
Gender nvarchar(10)
)
Go

Step 1 : Create User-defined Table Type

CREATE TYPE EmpTableType AS TABLE
(
Id INT PRIMARY KEY,
Name NVARCHAR(50),
Gender NVARCHAR(10)
)
Go

Step 2 : Use the User-defined Table Type as a parameter in the stored procedure. Table valued parameters must be passed as read-only to stored procedures, functions etc. This means you cannot perform DML operations like INSERT, UPDATE or DELETE on a table-valued parameter in the body of a function, stored procedure etc.

CREATE PROCEDURE spInsertEmployees
@EmpTableType EmpTableType READONLY
AS
BEGIN
INSERT INTO Employees
SELECT * FROM @EmpTableType
END

Step 3 : Declare a table variable, insert the data and then pass the table variable as a parameter to the stored procedure.

DECLARE @EmployeeTableType EmpTableType

INSERT INTO @EmployeeTableType VALUES (1, ‘Mark’, ‘Male’)
INSERT INTO @EmployeeTableType VALUES (2, ‘Mary’, ‘Female’)
INSERT INTO @EmployeeTableType VALUES (3, ‘John’, ‘Male’)
INSERT INTO @EmployeeTableType VALUES (4, ‘Sara’, ‘Female’)
INSERT INTO @EmployeeTableType VALUES (5, ‘Rob’, ‘Male’)

EXECUTE spInsertEmployees @EmployeeTableType

That’s it. Now select the data from Employees table and notice that all the rows of the table variable are inserted into the Employees table.

In our next video, we will discuss how to pass table as a parameter to the stored procedure from an ADO.NET application

Text version of the video
http://csharp-video-tutorials.blogspot.com/2015/09/table-valued-parameters-in-sql-server.html

Slides
http://csharp-video-tutorials.blogspot.com/2015/09/table-valued-parameters-in-sql-server_17.html

All SQL Server Text Articles
http://csharp-video-tutorials.blogspot.com/p/free-sql-server-video-tutorials-for.html

All SQL Server Slides
http://csharp-video-tutorials.blogspot.com/p/sql-server.html

All Dot Net and SQL Server Tutorials in English
https://www.youtube.com/user/kudvenkat/playlists?view=1&sort=dd

All Dot Net and SQL Server Tutorials in Arabic
https://www.youtube.com/c/KudvenkatArabic/playlists

Get Paid Taking Pictures
Share