<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Edwin M Sarmiento&gt;Table Valued Parameters in SQL Server 2008 &#8211; Edwin M Sarmiento</title>
	<atom:link href="https://www.edwinmsarmiento.com/table-valued-parameters-in-sql-server-2008/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.edwinmsarmiento.com</link>
	<description>Intentional Excellence</description>
	<lastBuildDate>Mon, 13 Apr 2026 21:00:49 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
<site xmlns="com-wordpress:feed-additions:1">84283043</site>		<item>
		<title>&gt;Table Valued Parameters in SQL Server 2008</title>
		<link>https://www.edwinmsarmiento.com/table-valued-parameters-in-sql-server-2008/</link>
		<comments>https://www.edwinmsarmiento.com/table-valued-parameters-in-sql-server-2008/#respond</comments>
		<pubDate>Mon, 12 Nov 2007 11:14:00 +0000</pubDate>
		<dc:creator>Edwin M Sarmiento</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<guid isPermaLink="false">http://bassplayerdoc.wordpress.com/2007/11/12/table-valued-parameters-in-sql-server-2008</guid>

				<description><![CDATA[&#62;As developers, there is always a need to pass data structures in one form or another in any programming language. TSQL is no exemption. This has been addressed by some workarounds as OPENXML in SQL Server 2000 where you can pass data around as VARCHAR data type. in SQL Server 2005, you have the XML [&#8230;]]]></description>
					<content:encoded><![CDATA[<p>&gt;<span style="font-family:arial;">As developers, there is always a need to pass data structures in one form or another in any programming language. TSQL is no exemption. This has been addressed by some workarounds as OPENXML in SQL Server 2000 where you can pass data around as VARCHAR data type. in SQL Server 2005, you have the XML data type where you can store data in XML form in a database column and use XPath with XQuery to manipulate the data. Both of which can, indeed, help pass data structures but still needs a lot of work to make it happen. Another approach is to create a temporary table on demand, populate the table before actually using the data in a stored procedure or function. SQL Server 2008 offers you the capabilities of passing a table as a parameter in a stored procedure and a function. Imagine populating a table on a client application and pass the table as a parameter to the backend database at one shot, thereby eliminating a lot of roundtrips, not to mention a couple of lines of codes. To understand how it works, let&#8217;s have a look at a sample code. First, I&#8217;ll create a very simple table named <strong><span style="font-size:85%;">TableTVP1</span></strong> which will be our target table.</span><br /><span style="font-family:Arial;"></span><br /><span style="font-family:lucida grande;font-size:85%;color:#006600;"><strong>&#8211;create sample table</strong></span><br /><span style="font-family:lucida grande;font-size:85%;"><strong><span style="color:#000099;">CREATE TABLE</span> [dbo].[TableTVP1]</strong></span><br /><span style="font-family:lucida grande;font-size:85%;"><strong>(rowID <span style="color:#000099;">int</span>)</strong></span><br /><span style="font-family:lucida grande;font-size:85%;"><strong>GO</strong></span><br /><span style="font-family:Arial;"></span><br /><span style="font-family:Arial;">The first step in understanding the concept of a table-valued parameter is to create a table-type; a new parameter which is of type <strong><span style="font-size:85%;">table</span></strong>. </span><br /><span style="font-family:Arial;"></span><br /><span style="font-family:lucida grande;font-size:85%;color:#006600;"><strong>&#8211;Create TABLE type</strong></span><br /><span style="font-family:lucida grande;font-size:85%;"><strong><span style="color:#000099;">CREATE TYPE</span> tvp_TableTVP1 <span style="color:#000099;">AS TABLE</span></strong></span><br /><span style="font-family:lucida grande;font-size:85%;"><strong>(rowID <span style="color:#000099;">int</span>)</strong></span><br /><span style="font-family:lucida grande;font-size:85%;"><strong>GO</strong></span></p>
<p><span style="font-family:arial;">The good thing about creating a table-type is that you can re-use it similar to re-using a user-defined data type. This means, you can use it to access any table with similar structure using the table-type(imagine having tables like Employees, Customers, Partners, etc. having similar table structures). After we have created the table-type, we can now use it it a stored procedure or function. The code below illustrates the use of the table-type we&#8217;ve just created</span><br /><span style="font-family:Arial;"></span><br /><strong><span style="font-size:85%;color:#006600;">&#8211;Setup stored procedure to accept the new TABLE type</span></strong><br /><strong><span style="font-size:85%;"><span style="color:#000099;">CREATE PROCEDURE</span> usp_tvpInsert (@tableparam tvp_TableTVP1 READONLY)</span></strong><br /><strong><span style="font-size:85%;color:#000099;">AS</span></strong><br /><strong><span style="font-size:85%;"><span style="color:#000099;">INSERT</span> TableTVP1</span></strong><br /><strong><span style="font-size:85%;"><span style="color:#3333ff;"><span style="color:#000099;">SELECT</span> </span>* FROM @tableparam</span></strong><br /><strong><span style="font-size:85%;">GO</span></strong></p>
<p><span style="font-family:arial;">Notice how we used the table-type<strong><span style="font-size:85%;"> tvp_Table1TVP1</span></strong> as a type to define the parameter <strong><span style="font-size:85%;">@tableparam</span></strong>. It&#8217;s just like any parameter you pass to a stored procedure or function except that it is defined as a table. The <strong><span style="font-size:85%;">READONLY</span></strong> attribbute passed simply tells us that a table-type passed as a parameter cannot be modified and no DML operations are permitted unlike when dealing with temporary tables. This means you need to do the data manipulation outside of this stored procedure or function before you can pass the table-type. Now that we have both the table-type and the stored procedure, let&#8217;s see how it works. </span></p>
<p><strong><span style="font-size:85%;color:#336666;">&#8211;Use the stored procedure</span></strong><br /><strong><span style="font-size:85%;"><span style="color:#000099;">DECLARE </span>@tableparam tvp_TableTVP1<br /><span style="color:#000099;">DECLARE</span> @i <span style="color:#000099;">SMALLINT</span></span></strong><br /><strong><span style="font-size:85%;"><span style="color:#000099;">SET </span>@i = 1 </span></strong></p>
<p><strong><span style="font-size:85%;"><span style="color:#000099;">WHILE </span>(@i &lt;=1000)</span></strong><br /><strong><span style="font-size:85%;"><span style="color:#000099;">BEGIN </span><br /><span style="color:#000099;">INSERT</span> @tableparam <span style="color:#000099;">VALUES</span> (@i)<br /><span style="color:#000099;">SET</span> @i = @i + 1</span></strong><br /><span style="font-size:85%;"><strong><span style="color:#000099;">END</span></strong></span><br /><span style="font-size:85%;"><strong><span style="color:#000099;"><br /></span><span style="color:#000099;">EXEC</span> usp_tvpInsert @tableParam</strong></span><br /><span style="font-size:85%;"></span><br /></span><span style="font-family:arial;font-size:100%;">The code simply inserts records from 1 to 1000 on a table-type named <strong><span style="font-size:85%;">@tableparam</span></strong> and passed it to the stored procedure. Imagine doing this in your client application and passing it one-time as a parameter in a stored procedure. It reduces the number of server roundtrips and improves response time aside from benefitting from being able to pass a table as a parameter. Check out my SQL Server 2008 videos at <a href="http://blogcastrepository.com/level5/sql2008/">BlogCastRepository.com</a> where a .NET application is also available for download as a sample that uses TVP</span><br /></span></p>
<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/92377218009570869-1151866804295386766?l=bassplayerdoc.blogspot.com' alt='' /></div>
]]></content:encoded>
			

		<wfw:commentRss>https://www.edwinmsarmiento.com/table-valued-parameters-in-sql-server-2008/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
				<post-id xmlns="com-wordpress:feed-additions:1">50</post-id>	</item>
	</channel>
</rss>