Behold the turtle

jb's Blog

"behold the turtle, he only makes progress when he sticks his neck out"
Table Value Parameters in SQL Server 2008 09 Sep 2007 13:11

Table Value Parameters are a small new T-SQL enhancement in SQL Server 2008 which allows us to pass through a TABLE value instance as a parameter to a stored procedure. This is a welcome new addition to support of the TABLE type as it helps in the situations where we want to pass in a row like structure without having to separate it out into arguments and then recombining it as part of the query.

Making use of it is fairly simple, let’s look at an example:

First we define the type

create type OrderTableType  as TABLE(ProductId int, ProductName varchar(255), Quantity int)

Then we define the stored procedure (implementation excluded)

create procedure dbo.UpdateProductFromDailyOrders( @orders OrderTableType READONLY )

 

First we need to declare an instance of the type and populate it with some data

declare @todaysOrders OrderTableType

insert into @todaysOrders

select p.Id, p.ProductName, sum(o.Quantity)

                from [Order] o inner join Product p on o.ProductId = p.Id

                where o.OrderDate = '2007-01-05'

                group by p.Id, p.ProductName

 

Then can now make use of it with the stored procedure

exec dbo.UpdateProductFromDailyOrders @todaysOrders

 

Easy!

The only caveat you have is that the structure passed in has to be specified as READONLY preventing any modification of the data inside the parameter.



comments (2)

history


2006 (47)
2007 (79)
January 2008 (5)
February 2008 (17)
March 2008 (7)
April 2008 (4)
May 2008 (1)
June 2008 (6)
August 2008 (1)

blogs i read


JDs Weblog
Andrews Blog
Chris Auld
Josh Robb
Ivan Porto Carrero
Rod Drury
Nic Wise
Alex James
Darryl Burling (MS)
Nigel Parker (MS)
Sean McBreen (MS)

links


Mindscape
Intergen
Microsoft Regional Director Microsoft MVP - SQL Server

RSS Feed rss feed