Click here to Skip to main content
11,577,522 members (39,057 online)
Click here to Skip to main content

Tagged as

Passing a datatable to a Stored Procedure in SQL Server 2008

, 25 Jun 2011 CPOL 71.1K 19
Rate this:
Please Sign up or sign in to vote.
Passing a datatable to a Stored Procedure in SQL Server 2008 which helps improve performance.

Create a table:

CREATE TABLE dbo.TestTableVariable
(
    ID        INT IDENTITY(1,1)    primary key,
    Name    VARCHAR(10),
    Addr    VARCHAR(10)
)

Create the table data type:

CREATE TYPE dbo.TableVariable AS TABLE
(
    ID        INT
)

Insert some default data:

INSERT INTO dbo.TestTableVariable VALUES ('aaa', 'addr-aaa')
INSERT INTO dbo.TestTableVariable VALUES ('bbb', 'addr-bbb')
INSERT INTO dbo.TestTableVariable VALUES ('ccc', 'addr-ccc')
INSERT INTO dbo.TestTableVariable VALUES ('ddd', 'addr-ddd')

Create the Stored Procedure that has the table variable as the parameter:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE dbo.SelectTestTableVariable
(
    @TableVar dbo.TableVariable READONLY
)
AS
BEGIN
    SET NOCOUNT ON;
    
    SELECT ID, Name, Addr FROM dbo.TestTableVariable
    WHERE ID IN (SELECT ID FROM @TableVar)
        
END
GO

Here is the C# code for a console application:

static void Main(string[] args)
{
    DataSet dataset = new DataSet();
    DataTable dataTable = new DataTable();
    dataTable.Columns.Add("ID");
    var dr = dataTable.NewRow();
    dr["ID"] = 1;
    dataTable.Rows.Add(dr);
    using (SqlConnection conn = new SqlConnection(
      @"Data Source=.\SqlExpress;Initial Catalog=Work;Integrated Security=True;"))
    {
        conn.Open();       
        using (SqlCommand cmd = new SqlCommand("[dbo].[SelectTestTableVariable]"))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = conn;
            cmd.Parameters.AddWithValue("@TableVar", dataTable);
            using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
            {
                adp.Fill(dataset);
            }
        }
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

zvin
Canada Canada
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionGetting error when calling the SP using enterprise library Pin
Nitin Sawant20-Nov-14 22:09
professionalNitin Sawant20-Nov-14 22:09 
GeneralMy vote of 4 Pin
abhi_here7-Sep-13 19:35
memberabhi_here7-Sep-13 19:35 
GeneralThe select in the SP should be: SELECT ID, Name, Addr FROM d... Pin
vladsoti28-Jun-11 11:46
membervladsoti28-Jun-11 11:46 
GeneralPerformance comparison for DataTable -> xml convertion? Pin
Burak Donbay28-Jun-11 2:05
memberBurak Donbay28-Jun-11 2:05 
GeneralReason for my vote of 5 Superb, new way, useful Pin
ramakrishnankt27-Jun-11 20:51
memberramakrishnankt27-Jun-11 20:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150603.1 | Last Updated 25 Jun 2011
Article Copyright 2011 by zvin
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid