<?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>Cowburn &#187; SQL</title>
	<atom:link href="http://cowburn.info/category/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://cowburn.info</link>
	<description>Online storage depot for Peter Cowburn</description>
	<lastBuildDate>Thu, 22 Jul 2010 17:51:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>T-SQL &#8211; Using a parameter in SELECT TOP clause</title>
		<link>http://cowburn.info/2007/03/15/select-top-parameter/</link>
		<comments>http://cowburn.info/2007/03/15/select-top-parameter/#comments</comments>
		<pubDate>Thu, 15 Mar 2007 15:13:31 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Select Top]]></category>
		<category><![CDATA[Stored Procedures]]></category>

		<guid isPermaLink="false">http://cowburn.info/web-coding/select-top-parameter/</guid>
		<description><![CDATA[I&#8217;ve been working with Stored Procedures a lot recently, even though the whole idea was new to be before starting this job. Today I was trying to select the first n rows from a table, but wanted to be able to change n via a parameter in the procedure call. I thought something like the [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working with Stored Procedures a lot recently, even though the whole idea was new to be before starting this job. Today I was trying to select the first <code>n</code> rows from a table, but wanted to be able to change <code>n</code> via a parameter in the procedure call. I thought something like the following would work, but it throws an error when trying to use the variable for the TOP clause.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" ><span style="color: #993333; font-weight: bold;">CREATE</span> PROCEDURE dbo<span style="color: #66cc66;">.</span>sp_TestGetAll
	@<span style="color: #993333; font-weight: bold;">LIMIT</span> int
<span style="color: #993333; font-weight: bold;">AS</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> TOP @<span style="color: #993333; font-weight: bold;">LIMIT</span>
	id<span style="color: #66cc66;">,</span> col1<span style="color: #66cc66;">,</span> col2<span style="color: #66cc66;">,</span> col3
<span style="color: #993333; font-weight: bold;">FROM</span> TestTable</pre></div></div>

<p>After a brief Google search there were a few methods presented including such nasties as manually writing out a SQL string within the procedure and executing that instead! Also, it was suggested to change the ROWCOUNT variable before and after issuing the select.  Sheesh, that&#8217;s messy.</p>
<p>Well anyway, the solution which works for me and is much easier is simply to wrap the variable in parenthesis and magic&#8230; it works.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" ><span style="color: #993333; font-weight: bold;">CREATE</span> PROCEDURE dbo<span style="color: #66cc66;">.</span>sp_TestGetAll
	@<span style="color: #993333; font-weight: bold;">LIMIT</span> int
<span style="color: #993333; font-weight: bold;">AS</span>
<span style="color: #808080; font-style: italic;">-- Notice the parentheses around @Limit - that is the only change!</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> TOP <span style="color: #66cc66;">&#40;</span>@<span style="color: #993333; font-weight: bold;">LIMIT</span><span style="color: #66cc66;">&#41;</span> 
	id<span style="color: #66cc66;">,</span> col1<span style="color: #66cc66;">,</span> col2<span style="color: #66cc66;">,</span> col3
<span style="color: #993333; font-weight: bold;">FROM</span> TestTable</pre></div></div>

<p>I&#8217;m just really noting this down so that I don&#8217;t forget, do another Google search and then resort to those other ugly ways of doing this simple task.</p>
]]></content:encoded>
			<wfw:commentRss>http://cowburn.info/2007/03/15/select-top-parameter/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
	</channel>
</rss>
