Monday, December 1, 2008

TVP (Table Value Parameter - SQL 2008)

This is a excellent feature provided in SQL Server 2008. Now in SQL Server 2008 we can pass a table as a parameter to stored procedure.Let me explain with an example.
I need to pass a collection of rows to a procedure. The collection of rows is the output of many joins between the tables with the select statement. Within the procedure we have to some kind of manipulation with the set of rows. In SQL 2005 there is no such facility to pass a table as a parameter to the procedure. But in SQL 2008 we have the facility to pass a table as a parameter to the procedure called Table Value Parameter.
Consider that the output of the query is stored in the below table.
CREATE TYPE CustomerType AS TABLE (Name varchar(20), Age int)
The procedure is created as below where the @Customer is table value parameter
CREATE PROCEDURE sp_MySproc(@Customer CustomerType READONLY)
Finally, call the stored procedure using the below statement. Where the @Customer is a table which is having set of rows.
exec sp_MySproc @Customer
by this way you can use the TVP as more powerful.
I hope it will help you guys..

No comments: