Introducing Table-Valued Parameters - Part 1

Published 15 August 08 04:37 PM | dpblogs 

Hey everyone!

I am Himanshu. I am a program manager in the ADO.Net team. Today I am going to talk about Table-Valued parameters that are new in SQL Server 2008. About 4 years back I was writing an application which involved adding metadata about my music library into a database. The database had a table each for storing Artists, Albums, and Songs. While adding an Album of songs into the database I had to insert data into each of these tables. I had defined stored procedure for inserting data into each of the tables. In the first iteration there was a bug in the code due to which these operations were not being done in a single transaction. Needless to say this bug surfaced as a data-corruption issue later on. Once identified the fix was trivial, ensure that all commands are executed in a single transaction. However, what I would have really liked to do was to be able to call a single stored procedure to which I could pass the metadata about the Album and the list of songs at the same time. But there was no good way of accomplishing this back then. I was programming against SQL Server 2000 back then. I am excited to say that now with the new table-valued parameters in SQL Server 2008 I can finally write a single stored procedure that will let me accomplish this without any kind of hacks!

Table-Valued parameters, as the name suggests lets you pass a table as parameter to a stored procedure. In order to use table-valued parameters you need to define a table type and then use the table type in the definition of the stored procedure. Let me illustrate this with the help of an example. I am going to first create a table type named Songs_TableType with two columns, Title and TrackNumber. Then I will use it in a stored procedure to pass a table as parameter.

First creating the table type

Create Type Songs_TableType as Table
(Title nvarchar(120) not null,
TrackNumber int)

I can now use this type in a stored procedure to pass a table as a parameter. The following T-Sql shows how to define a stored procedure that takes this type as a parameter. Note that I have skipped error handling for brevity.

create procedure AddSongs(
 @ArtistName nvarchar(120), 
 @AlbumName nvarchar(120), 
 @Songs Songs_TableType READONLY)
as
begin
 -- Add the Artist
 Declare @ArtistID int
 insert into Artists values (@ArtistName)
 select @ArtistID = SCOPE_IDENTITY()

 -- Add the Album
 Declare @AlbumID int
 insert into Albums values (@AlbumName, @ArtistID)
 select @AlbumID = SCOPE_IDENTITY()

 -- Insert songs
 insert into Songs 
 select title, trackNumber, @AlbumID, @ArtistID
 from @Songs
end 

In the next post I will go into more details into how I can use the stored procedure defined above to simplify the code I have on the client. Till then, adios!

Himanshu Vasishth
Program Manager
ADO.NET

Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# discount furniture » Introducing Table-Valued Parameters - Part 1 said on August 15, 2008 7:52 PM:

PingBack from http://informationsfunnywallpaper.cn/?p=866

# wisemx said on August 15, 2008 8:19 PM:

Thanks Himanshu...

Keep me posted and I'll create a series of Silverlight streaming Webcasts for these.

 Salute,

   Mark Wisecarver

   Technical Evangelist

# Michael O'Neill said on August 15, 2008 10:21 PM:

This sounds like a better feature than it will turn out to be in the end.  I remember when Oracle introduced this feature a decade ago and it just never became a common feature to use in all that time.

# BlackTiger said on August 16, 2008 6:45 AM:

1. You deserve to ... for such feature

2. How to work with this...feature from .NET?

3. Some "smarties" used such feature in some Oracle app. And I was involved to build .NET "bridge" for it... It was a disaster...

# danieldsmith said on August 18, 2008 7:26 AM:

Table Valued Parameters are incredibly useful.  We have a number of apps where we previously had to pass in comma separated lists of IDs (e.g. list of Customer IDs) and then parse the strings and turn them into temporary tables so we could do joins on them.

It would be really neat if there were some built in tables types defined so we didn't have to create types like these every time:

CREATE TYPE IntList_TableType AS TABLE (ID int not null)

CREATE TYPE BigIntList_TableType AS TABLE (ID bigint not null)

# gOODiDEA said on August 21, 2008 7:07 AM:

.NETTCP:BufferManagementASP.NetLoadTestingandOptimizationToolkit-Soyouwanttobeahe...

# gOODiDEA.NET said on August 21, 2008 7:09 AM:

.NET TCP: Buffer Management ASP.Net Load Testing and Optimization Toolkit - So you want to be a hero

# John said on September 18, 2008 3:34 AM:

Hi,

Thanks Himanshu for your ideas and codings. Me too finished my .NET course and plan to do a project using ADO.NET. I think your sql parameters surely will help me out in my project to execute well.

Thank you very much...

--------------------

John

<a href="http://www.widecircles.ca">Wide Circles</a>

# cozqzrxqd said on January 23, 2009 1:03 PM:

IDCzFx  <a href="http://kklmaanqjbgq.com/">kklmaanqjbgq</a>, [url=http://oenzopxvjrfp.com/]oenzopxvjrfp[/url], [link=http://rqccweciopkk.com/]rqccweciopkk[/link], http://hasqfgeztdzw.com/

# Rukshan said on March 17, 2009 5:35 AM:

when you define a table,what are main parameters that you are considering? plese send a anser from mail to dewamax@gamil.com

Leave a Comment

(required) 
(optional)
(required) 
Page view tracker