<?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>Simon Whatley &#187; tabular</title>
	<atom:link href="http://www.simonwhatley.co.uk/tag/tabular/feed" rel="self" type="application/rss+xml" />
	<link>http://www.simonwhatley.co.uk</link>
	<description>The opposite of every great idea is another great idea</description>
	<lastBuildDate>Wed, 02 Nov 2011 09:28:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>SQL User-Defined Function: ListToTable</title>
		<link>http://www.simonwhatley.co.uk/sql-user-defined-function-listtotable</link>
		<comments>http://www.simonwhatley.co.uk/sql-user-defined-function-listtotable#comments</comments>
		<pubDate>Mon, 22 Sep 2008 10:58:30 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database server]]></category>
		<category><![CDATA[extend]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[sub-routine]]></category>
		<category><![CDATA[subroutine]]></category>
		<category><![CDATA[t-sql]]></category>
		<category><![CDATA[tabular]]></category>
		<category><![CDATA[UDF]]></category>
		<category><![CDATA[user defined function]]></category>

		<guid isPermaLink="false">http://www.simonwhatley.co.uk/?p=426</guid>
		<description><![CDATA[A common need in SQL is the ability to iterate over a list as if it were an array. In SQL it is not possible to declare arrays, unlike other programming languages such as ColdFusion, ActionScript and Java. Fortunately, there is a way around this problem: use a User-Defined Functions (UDFs) to create a tabular version of the data. Arrays are, after all, essentially tabular data (at their simplest, one dimension level).]]></description>
			<content:encoded><![CDATA[<p>A common need in <abbr title="Structured Query Language">SQL</abbr> is the ability to iterate over a list as if it were an <a href="http://en.wikipedia.org/wiki/Array" title="Wikipedia: Array" target="_blank" rel="nofollow">array</a>. In <abbr title="Structured Query Language">SQL</abbr> it is not possible to declare arrays, unlike other programming languages such as ColdFusion, ActionScript and Java. Fortunately, there is a way around this problem: use a <a href="http://en.wikipedia.org/wiki/User-defined_function" title="Wikipedia: User-Defined Functions" target="_blank" rel="nofollow">User-Defined Functions</a> (<abbr title="User Defined Functions">UDF</abbr>s) to create a tabular version of the data. Arrays are, after all, essentially tabular data (at their simplest, one dimension level).</p>
<p>A User-Defined Function, is a function provided by the user of a program or environment. In <abbr title="Structured Query Language">SQL</abbr> databases, a user-defined function provides a mechanism for extending the functionality of the database server by adding a function that can be evaluated in <abbr title="Structured Query Language">SQL</abbr> statements.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<h3>The Function Code</h3>
<p>Below is the complete function definition:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><<span style="color: #0000ff;">|</span>/1/>CREATE</span> <<span style="color: #0000ff;">|</span>/1/>FUNCTION</span> dbo.udf_ListToTable
<span style="color: #66cc66;">&#40;</span>
	@LIST 		<<span style="color: #0000ff;">|</span>/2/>NVARCHAR</span><span style="color: #66cc66;">&#40;</span><<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">4000</span><span style="color: #66cc66;">&#41;</span>,
	@DELIMITER 	<<span style="color: #0000ff;">|</span>/2/>NVARCHAR</span><span style="color: #66cc66;">&#40;</span><<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #0000ff;">=</span> <span style="color: #ff0000;">','</span>
<span style="color: #66cc66;">&#41;</span>
<<span style="color: #0000ff;">|</span>/1/>RETURNS</span> @ListTable <<span style="color: #0000ff;">|</span>/1/>TABLE</span>
<span style="color: #66cc66;">&#40;</span>
	Item <<span style="color: #0000ff;">|</span>/2/>NVARCHAR</span><span style="color: #66cc66;">&#40;</span><<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">200</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>
<<span style="color: #0000ff;">|</span>/1/>AS</span>
<<span style="color: #0000ff;">|</span>/1/>BEGIN</span>
	<<span style="color: #0000ff;">|</span>/1/>DECLARE</span> @LenDel 	<<span style="color: #0000ff;">|</span>/2/>INT</span>
	<<span style="color: #0000ff;">|</span>/1/>DECLARE</span> @Pos 		<<span style="color: #0000ff;">|</span>/2/>INT</span>
	<<span style="color: #0000ff;">|</span>/1/>DECLARE</span> @Item 		<<span style="color: #0000ff;">|</span>/2/>NVARCHAR</span><span style="color: #66cc66;">&#40;</span><<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">200</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
	<span style="color: #808080;">--Get the length of the delimiter, use hack to get around LEN(' ') = 0 issue</span>
	<<span style="color: #0000ff;">|</span>/1/>SET</span> @LenDel <span style="color: #0000ff;">=</span> <span style="color: #ff00ff;">LEN</span><span style="color: #66cc66;">&#40;</span>@DELIMITER <span style="color: #0000ff;">+</span> <span style="color: #ff0000;">'|'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #0000ff;">-</span> <<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">1</span>
&nbsp;
	<<span style="color: #0000ff;">|</span>/1/>SET</span> @Pos <span style="color: #0000ff;">=</span> <span style="color: #ff00ff;">CHARINDEX</span><span style="color: #66cc66;">&#40;</span>@DELIMITER, @LIST<span style="color: #66cc66;">&#41;</span>
	<<span style="color: #0000ff;">|</span>/1/>WHILE</span> @Pos <span style="color: #66cc66;">&gt;</span> <<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">0</span>
	<<span style="color: #0000ff;">|</span>/1/>BEGIN</span>
		<span style="color: #808080;">--Get the item</span>
		<<span style="color: #0000ff;">|</span>/1/>SET</span> @Item <span style="color: #0000ff;">=</span> <span style="color: #ff00ff;">SUBSTRING</span><span style="color: #66cc66;">&#40;</span>@LIST, <<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">1</span>, @Pos-<<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #808080;">--Add it to the table (if not empty string)</span>
		<<span style="color: #0000ff;">|</span>/1/>IF</span> <span style="color: #ff00ff;">LEN</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff00ff;">LTRIM</span><span style="color: #66cc66;">&#40;</span>@Item<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">0</span>
			<<span style="color: #0000ff;">|</span>/1/>INSERT</span> @ListTable <span style="color: #66cc66;">&#40;</span>Item<span style="color: #66cc66;">&#41;</span> <<span style="color: #0000ff;">|</span>/1/>VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff00ff;">LTRIM</span><span style="color: #66cc66;">&#40;</span>@Item<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #808080;">--Remove the item from the list</span>
		<<span style="color: #0000ff;">|</span>/1/>SET</span> @LIST <span style="color: #0000ff;">=</span> <span style="color: #ff00ff;">STUFF</span><span style="color: #66cc66;">&#40;</span>@LIST, <<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">1</span>, @Pos+@LenDel-<<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">1</span>, <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #808080;">--Get the position of the next delimiter</span>
		<<span style="color: #0000ff;">|</span>/1/>SET</span> @Pos <span style="color: #0000ff;">=</span> <span style="color: #ff00ff;">CHARINDEX</span><span style="color: #66cc66;">&#40;</span>@DELIMITER, @LIST<span style="color: #66cc66;">&#41;</span>
	<<span style="color: #0000ff;">|</span>/1/>END</span>
&nbsp;
	<span style="color: #808080;">--Add the last item to the table (if not empty string)</span>
	<<span style="color: #0000ff;">|</span>/1/>IF</span> <span style="color: #ff00ff;">LEN</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff00ff;">LTRIM</span><span style="color: #66cc66;">&#40;</span>@LIST<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">0</span>
		<<span style="color: #0000ff;">|</span>/1/>INSERT</span> @ListTable <span style="color: #66cc66;">&#40;</span>Item<span style="color: #66cc66;">&#41;</span> <<span style="color: #0000ff;">|</span>/1/>VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff00ff;">LTRIM</span><span style="color: #66cc66;">&#40;</span>@LIST<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
	<<span style="color: #0000ff;">|</span>/1/>RETURN</span>
<<span style="color: #0000ff;">|</span>/1/>END</span>
<<span style="color: #0000ff;">|</span>/1/>GO</span></pre></div></div>

<p>The function simply loops over the list passed into the function. Each list item is then inserted into the variable named <code>@ListTable</code>, which is of type <code>TABLE</code>. The <code>@ListTable</code> variable is then returned out of the function and can be handled the same as any other table.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<h3>The Function In Use</h3>
<p>A simple demonstration is as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><<span style="color: #0000ff;">|</span>/1/>INSERT</span> <<span style="color: #0000ff;">|</span>/1/>INTO</span> tableName <span style="color: #66cc66;">&#40;</span>column1, column2, column3, column4<span style="color: #66cc66;">&#41;</span>
<<span style="color: #0000ff;">|</span>/1/>SELECT</span> @variable1, @variable2, myTable.item, <span style="color: #ff00ff;">GETDATE</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<<span style="color: #0000ff;">|</span>/1/>FROM</span> dbo.udf_ListToTable<span style="color: #66cc66;">&#40;</span>@list,<span style="color: #ff0000;">','</span><span style="color: #66cc66;">&#41;</span> <<span style="color: #0000ff;">|</span>/1/>AS</span> myTable</pre></div></div>

<p>In this example, we insert the same information (<code>@variable1</code>, <code>@variable2</code>) for every instance of an item found in <code>myTable</code>.</p>
<p>This is useful, for example, if you want to apply a setting to a group of users. The group of users could be contained in a list that needs to be parsed as a table, whilst the individual setting details are contained in the other variables.</p>
<h3>Download the Code</h3>
<p><a href="/examples/sql/functions/udf_ListToTable.txt" title="Download the code">Download the code</a>, rename the file to .sql and run on your database instance. You will then be able to reference the function in your Stored Procedures.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.simonwhatley.co.uk/sql-user-defined-function-listtotable/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

