![]() |
|
Identify and resolve SQL Server problems before they happen

|
|
By: John Sterrett | Read Comments (4) | Related Tips: More > Table Valued Parameters |
We recently upgraded our database to SQL Server 2008 and I want to update our data access C# code to use Table Value Parameters with our stored procedures. In a previous tip I saw that Table Value Parameters were used with a Data Warehousing example. Can you show me how to implement Table Value Parameters with my .NET Application to insert multiple records using one round-trip?
Table Value Parameters is a new feature for developers in SQL Server 2008. It allows you to pass read-only table variables into a stored procedure. In the past I have used a comma delimited string or XML to simulate this purpose. I would have to parse out the string into a temp table similar to this example.
Once you have the table parameter passed into the stored procedure you can leverage the Table Value Parameter just like any other table except you cannot modify the parameter as it’s read-only. Below we will apply a common example to bulk insert data using one round trip.
The following is a sample table that we will use in this example to insert items.
CREATE TABLE [dbo].[Items]( [ItemID] [int] NOT NULL, [Name] [nvarchar](50) NULL, CONSTRAINT [PK_Items] PRIMARY KEY CLUSTERED ( [ItemID] ASC )) ON [PRIMARY]
In order to use table value parameters you have to create a table type. The table type is used to describe the structure of the table value parameter. This is similar to making a strong type.
CREATE TYPE [dbo].[TVP_Items] AS TABLE( [ItemID] [int] NOT NULL, [Name] [nvarchar](50) NULL ) GO
Now that you have a table type you have to declare an instance to use it. The following is an quick example that declares an instance of the table type an populates it with some values. Later we will use C# to create a data table that will populate an instance of a table type.
-- Declare instance of TVP_Items Table Type DECLARE @TVP TVP_Items -- Add some sample values into our instance of Table Type. INSERT INTO @TVP (ItemID, Name) VALUES (1, 'Hat'), (2, 'T-Shirt'), (3, 'Football'), (4, 'Jersey') -- show values that exist in table type instance. SELECT * FROM @TVP
In order to use table value parameters in a stored procedure you have to declare an instance of the table type and specify it as read-only. It is mandatory for the table value parameter to be read-only so you cannot modify the data inside of the table type variable inside the stored procedure.
Below we will insert the data in the table value parameter into the dbo.items table.
CREATE PROCEDURE [dbo].[InsertItemsTVP] @ItemTVP TVP_Items READONLY AS BEGIN INSERT INTO dbo.Items (ItemID, Name) SELECT ItemID, Name FROM @ItemTVP END GO
The following code below generates a data table in C# and it includes four rows. A data table is a common data type used to simulate a table. This data table will be used as our table value parameter and will be used by the stored procedure created above. The data table is not the only type that can be used for table value parameters in C#. The DataReader and list types are also acceptable.
DataTable _dt;
// create data table to insert items
_dt = new DataTable("Items");
_dt.Columns.Add("ItemID", typeof(string));
_dt.Columns.Add("Name", typeof(string));
_dt.Rows.Add(4, "SuperBowl 9 Hat");
_dt.Rows.Add(5, "SuperBowl 10 T-Shirt");
_dt.Rows.Add(6, "SuperBowl 13 Towel");
_dt.Rows.Add(7, "SuperBowl 14 Helmet");Now that we have our data table created we can move on to the data access code. The majority of the code listed below will be the same for the majority of your data access code. The only difference is we will specify that the input type is sql data type as structured and we will pass in our data table to the input parameter. The two lines you need to focus on are underlined in the code section below.
SqlConnection con;
// modify connection string to connect to your database
string conStr = "Server=localhost;Database=MSSQLTIPS;Trusted_Connection=True;";
con = new SqlConnection(conStr);
con.Open();
using (con)
{
// Configure the SqlCommand and SqlParameter.
SqlCommand sqlCmd = new SqlCommand("dbo.InsertItemsTVP", con);
sqlCmd.CommandType = CommandType.StoredProcedure;
SqlParameter tvpParam = sqlCmd.Parameters.AddWithValue("@ItemTVP", _dt); //Needed TVP
tvpParam.SqlDbType = SqlDbType.Structured; //tells ADO.NET we are passing TVP
sqlCmd.ExecuteNonQuery();
}
con.Close();When you use this tip you can setup SQL Server Profiler to capture the .NET calls. You will see the following three calls. You should notice that they are very similar to the T-SQL calls above.

| Friday, February 18, 2011 - 2:13:01 PM - marek | Read The Tip |
|
Very good article. I was that I was looking for. I saved a lot of time and wrote a piece of good code. Thank you very mych. Marek Janowski |
|
| Friday, February 18, 2011 - 3:10:19 PM - John Sterrett | Read The Tip |
|
Marek, You are welcome. I am happy to know that this tip was helpful for you. |
|
| Wednesday, March 28, 2012 - 12:01:50 PM - Charles Foushee | Read The Tip |
|
Question about the datatable you are passing as a parameter: Does it have to have EXACTLY the same columns as the Table type or can it have additional columns? From your example, if the data table you constructed look like below, would it still work? |
|
| Friday, May 18, 2012 - 12:03:48 AM - cooljack | Read The Tip |
|
how could i do that using gridview. when i select checkbox in gridview all i want is to save multiple record at once...
|
|
|
privacy | disclaimer | copyright | advertise | about authors | contribute | feedback | giveaways | user groups Some names and products listed are the registered trademarks of their respective owners. Edgewood Solutions LLC | MSSharePointTips.com | MSSQLTips.com |