In this article, we explore the SQL server 2008 Table value parameters. Many articles have been published on this subject, some describe the SQL server side of things, others discuss how to access the table value functions from .NET.
I was unable to find one crisp article just describing a simple example of how to use Table value parameters using C# and SQL Server 2008 and after some research, I wrote this article so that other individuals may benefit from my findings.
Table value parameters are a simple mechanism to pass bulk of data from ADO.NET to SQL Server. They allow data to be available on the SQL Server as a temporary table.
This task in the past was done using XML in which the bulk data was modelled in a hierarchical structure and passed to the stored procedure. The stored procedure then converted back the structure into a temporary table to process the data in a relational manner.
The audience of this article is expected to be aware of ADO.NET, C# and SQL Server 2008.
To demonstrate an end to end example of table value parameters.
We will directly jump into the code for this article. We shall discuses each layer from the SQL Server Table to the C# code.
The Database Table
In this section, we describe the table schema for the sample problem. The table in this code will be a simple table with an identity column along with a
integer column. The
nvarchar column will not allow
nulls and the integer will allow
nulls. The schema creations script for the same is:
CREATE TABLE dbo.SampleTable
Id int NOT NULL IDENTITY (1, 1),
SampleString nvarchar(64) NOT NULL,
SampleInt int NULL
) ON [PRIMARY]
The Table Value Parameter Type
To pass the data along as a table value parameter, a new database type has to be created. This type definition in essence describes what one row will look like and we will pass around a bunch of such rows. The script for the same is:
CREATE TYPE [dbo].[SampleDataType] As Table
SampleString Nvarchar(64) Not Null
, SampleInt Int
One may observe that the structure of this type is similar to the table, however what is important is that we need to create the type definition most convenient to pass along the data which may mean de-normalization.
The Stored Procedure
The stored procedure is a rather simple piece of code which takes the
SampleDataType type as input parameter. Inside the stored procedure, the parameter is available as a temporary table. This table however has to be readonly.
CREATE PROCEDURE [dbo].[SampleProcedure]
@Sample As [dbo].[SampleDataType] Readonly
Insert Into SampleTable(SampleString,SampleInt)
Select SampleString, SampleInt From @Sample
The DB Test Script
At this stage, we should quickly test our database for any errors using this script, which also describe the mechanism using which table value parameters may be passed along from one stored procedure to other.
Declare @SampelData As [dbo].[SampleDataType]
Insert Into @SampelData(SampleString, SampleInt) Values('1',1);
Insert Into @SampelData(SampleString, SampleInt) Values('2',null);
Insert Into @SampelData(SampleString, SampleInt) Values('3',3);
Select * From @SampelData
Exec SampleProcedure @SampelData
Select * From SampleTable
The C# Code
In the C# code, we have to resolve two problems to use the required stored procedure.
- Create a data structure which is equivalent to the table value parameter - just the way a
nvarchar is represented as
string on the C# end.
- Pass this data to the Stored procedure.
Representing the Data
There are many mechanisms available to represent the data such as representing the data as a
IEnumerable, Linq object, Data reader, etc. In this article, we will focus on the
DataTable. All we do is create a
DataTable, define columns parallel to our Table data type and fill them up.
DataTable dataTable = new DataTable("SampleDataType");
Passing the Data
To pass the data, it is to be represented as a
SqlParameter. The type of this parameter is
Structured. The details are as shown in the code snippet. The other code to call the SP is trivial and may be seen in the given code.
SqlParameter parameter = new SqlParameter();
parameter.SqlDbType = System.Data.SqlDbType.Structured;
parameter.Value = dataTable;
Please note that the attached code is developed in Visual Studio 2010 + SQL Server 2008 however it will work with Visual Studio 2005 and SQL Server 2008.
- 20th August, 2009: Initial post