Table valued parameters in SQL Server

Поделиться
HTML-код
  • Опубликовано: 8 сен 2024
  • 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 RUclips 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-tu...
    Slides
    csharp-video-tu...
    All SQL Server Text Articles
    csharp-video-tu...
    All SQL Server Slides
    csharp-video-tu...
    All Dot Net and SQL Server Tutorials in English
    www.youtube.co...
    All Dot Net and SQL Server Tutorials in Arabic
    / kudvenkatarabic

Комментарии • 30

  • @michaelnyberg3312
    @michaelnyberg3312 6 лет назад +4

    Thanks for sharing! Very well organized and right to the point. Really appreciate your effort!

  • @krzysztofs8535
    @krzysztofs8535 7 лет назад +4

    Thank you for this video! great tutorial. I'm the biggest fan of your tutorials. God bless you and keep goin'.

  • @Tall-Cool-Drink
    @Tall-Cool-Drink 4 года назад

    Very helpful video.
    Thank you.

  • @jayrichzhanra4988
    @jayrichzhanra4988 6 лет назад

    Thank you for your time and effort bro! We are learning!

  • @SQLDeveloperBI
    @SQLDeveloperBI 5 лет назад

    Thank you!

  • @MrJohn2475
    @MrJohn2475 5 лет назад

    thank you! this is great!

  • @raqibul1000
    @raqibul1000 9 лет назад

    Nice tutorial.Go Ahead.

  • @antydsr
    @antydsr 9 лет назад

    Great tutorial. And I have to do this - first! :D

  • @mohamednoordheen3558
    @mohamednoordheen3558 9 лет назад +1

    Sir.... Pls post videos on 3-Tier Architecture

  • @Ronald-zd8fs
    @Ronald-zd8fs 2 года назад +1

    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

  • @JonathanIsmaila
    @JonathanIsmaila 4 года назад +1

    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

  • @zombie4love
    @zombie4love 5 лет назад +1

    For me, after creating UDTT, I had to restart SSMS to be able to use it in the sp

  • @kidzfunkaustubh
    @kidzfunkaustubh 3 года назад

    Nice

  • @siddhikasar6717
    @siddhikasar6717 4 года назад

    Sir please post video on joint hints(Hash, Loop, Merge)

  • @jasmineRodriguez-hg6di
    @jasmineRodriguez-hg6di 9 лет назад +1

    Hi, can you please explain how we can alter a user defined table type? thnx :)

  • @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?

  • @sandeepmishra2622
    @sandeepmishra2622 9 лет назад

    Please add some videos regarding sql jobs

  • @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

  • @data.matters
    @data.matters 8 месяцев назад

    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 7 месяцев назад

      add step 2
      @EmployeeTableType"

  • @MDARIF-yo3tq
    @MDARIF-yo3tq 6 лет назад

    what is real time use of table variable

  • @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

  • @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.

  • @xst-k6
    @xst-k6 3 года назад

    Should have included a real world use case

  • @sultanatrangari6180
    @sultanatrangari6180 5 лет назад

    this is table valued parameter in stored procedure
    what about table valued parameter in functions

  • @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