Тёмный

Table valued parameters in SQL Server 

kudvenkat
Подписаться 829 тыс.
Просмотров 146 тыс.
50% 1

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.
Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our RU-vid channel. Hope you can help.
/ @aarvikitchen5572
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
csharp-video-tutorials.blogspo...
Slides
csharp-video-tutorials.blogspo...
All SQL Server Text Articles
csharp-video-tutorials.blogspo...
All SQL Server Slides
csharp-video-tutorials.blogspo...
All Dot Net and SQL Server Tutorials in English
ru-vid.com...
All Dot Net and SQL Server Tutorials in Arabic
/ kudvenkatarabic

Опубликовано:

 

16 сен 2015

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 30   
@michaelnyberg3312
@michaelnyberg3312 5 лет назад
Thanks for sharing! Very well organized and right to the point. Really appreciate your effort!
@krzysztofs8535
@krzysztofs8535 7 лет назад
Thank you for this video! great tutorial. I'm the biggest fan of your tutorials. God bless you and keep goin'.
@jayrichzhanra4988
@jayrichzhanra4988 6 лет назад
Thank you for your time and effort bro! We are learning!
@antydsr
@antydsr 8 лет назад
Great tutorial. And I have to do this - first! :D
@raqibul1000
@raqibul1000 8 лет назад
Nice tutorial.Go Ahead.
@Tall-Cool-Drink
@Tall-Cool-Drink 4 года назад
Very helpful video. Thank you.
@MrJohn2475
@MrJohn2475 5 лет назад
thank you! this is great!
@SQLDeveloperBI
@SQLDeveloperBI 5 лет назад
Thank you!
@JonathanIsmaila
@JonathanIsmaila 4 года назад
Please I have a question and I would so much love the get a response from you. Please when passing the datatable parameter to sql procedure, is it possible to check for errors just like you did in part 56 and 57 videos? For example, you checked if the quantity sold is not greater than quantity available. Is that also possible with datatable parameter?
@truonghuynhhoa
@truonghuynhhoa 5 лет назад
thanks a lot
@mohamednoordheen3558
@mohamednoordheen3558 8 лет назад
Sir.... Pls post videos on 3-Tier Architecture
@zombie4love
@zombie4love 5 лет назад
For me, after creating UDTT, I had to restart SSMS to be able to use it in the sp
@Ronald-zd8fs
@Ronald-zd8fs 2 года назад
Hi sir, i just found this tutorial and thanks for it. I have a question regarding the data type : if there are more than 1 execute the same SP ( spInsertEmployees ) at the same time what will happen to EmpTableType ? for exmaple : the first one ( like example) inserts 5 rows the second one Inserts 3 rows will the EmpTableType have 8 rows? or EmpTableType has 5 and 3 for each transaction ? Sory for bad english, i hope you undertand what i mean :) Thanks, Ronald
@mrgreenteatime2987
@mrgreenteatime2987 7 лет назад
Hi, thank you for this nice tutorial. Is it possiblet to use similar approach to pass list of records from excel into the store procedure?
@jasmineRodriguez-hg6di
@jasmineRodriguez-hg6di 8 лет назад
Hi, can you please explain how we can alter a user defined table type? thnx :)
@sandeepmishra2622
@sandeepmishra2622 8 лет назад
Please add some videos regarding sql jobs
@siddhikasar6717
@siddhikasar6717 3 года назад
Sir please post video on joint hints(Hash, Loop, Merge)
@kidzfunkaustubh
@kidzfunkaustubh 3 года назад
Nice
@saleem801
@saleem801 7 лет назад
If I insert new values into the table type and execute the sp for a second time, will it re-insert the values from the first execution?
@momen153
@momen153 6 лет назад
Saleem Khan yes of course
@statussatishgt8042
@statussatishgt8042 4 года назад
i need a help plz help me suppose i have 10 database in my table for example source to destination i wont one at a time when'll search suppose any one by 10 how can its posible
@MDARIF-yo3tq
@MDARIF-yo3tq 6 лет назад
what is real time use of table variable
@Anushkathecuteprincess
@Anushkathecuteprincess 2 года назад
Hi, i have one query, if suppose i want to insert 10 records and 9th record has some issue(like passing int type data instead of string) so this store procedure will not save all 10 records, how can we overcome this problem? plz help
@ashishchinna9201
@ashishchinna9201 Год назад
You are declaring a table variable and passing values into that table variable. This variable is passed as a parameter to the stored procedure. So the 9th record which has error will not be stored in your declared variable. Since the variable is incomplete it is not passed into the stored procedure and it won't be executed.
@sultanatrangari6180
@sultanatrangari6180 5 лет назад
this is table valued parameter in stored procedure what about table valued parameter in functions
@data.matters
@data.matters 6 месяцев назад
While executing Stored Proc, I am getting below error message, can any please help?? Msg 137, Level 15, State 2, Line 39 Must declare the scalar variable "@EmployeeTableType".
@PrantoBhoumik
@PrantoBhoumik 5 месяцев назад
add step 2 @EmployeeTableType"
@xst9880
@xst9880 3 года назад
Should have included a real world use case
@senolkurt7864
@senolkurt7864 5 лет назад
Why don't we simply insert data into the table directly? What's the gain?
@JonathanIsmaila
@JonathanIsmaila 4 года назад
If you have multiple records, that means you have to insert one after the other using loop function from your app. It won't be fine if the network is so slow
Далее
Send datatable as parameter to stored procedure
9:27
Просмотров 119 тыс.
The Ultimate Guide to Table-Valued Parameters (TVP)
34:24
🤯 #funny
00:20
Просмотров 366 тыс.
Макс Пэйн - ТРЕШ ОБЗОР на фильм
47:02
79 What are user defined functions in sql server
11:57
Просмотров 1,4 тыс.
Grouping function in SQL Server
12:49
Просмотров 79 тыс.
Table variable in SQL Server 2019 with examples
16:05
Просмотров 4,2 тыс.
58 Merge Statement in SQL Server
11:14
Просмотров 3,8 тыс.