<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Retrieve the top X random rows from a query</title>
	<atom:link href="http://facility9.com/2009/01/26/retrieve-the-top-x-rows-from-a-query/feed" rel="self" type="application/rss+xml" />
	<link>http://facility9.com/2009/01/26/retrieve-the-top-x-rows-from-a-query</link>
	<description>Jeremiah Peschka&#039;s ruminations on sql, ruby, c# and other things</description>
	<lastBuildDate>Thu, 11 Mar 2010 16:05:37 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Jeremiah Peschka</title>
		<link>http://facility9.com/2009/01/26/retrieve-the-top-x-rows-from-a-query#comment-183</link>
		<dc:creator>Jeremiah Peschka</dc:creator>
		<pubDate>Tue, 27 Jan 2009 12:45:33 +0000</pubDate>
		<guid isPermaLink="false">http://facility9.com/?p=289#comment-183</guid>
		<description>It actually turns out that the execution plans are almost exactly the same (in this case). Using TOP (@Count) is slightly more efficient within this scenario in AdventureWorks (the plan cost for my query is 0.412827 and for TOP it is 0.412769). The difference is made up by some additional operators and a filter that I&#039;m using to replace the functionality of TOP via my use of ROW_NUMBER and the WHERE clause.

Which brings up the point that we&#039;re always supposed to be using the parens with TOP and I just found that out this morning while reading Books Online.</description>
		<content:encoded><![CDATA[<p>It actually turns out that the execution plans are almost exactly the same (in this case). Using TOP (@Count) is slightly more efficient within this scenario in AdventureWorks (the plan cost for my query is 0.412827 and for TOP it is 0.412769). The difference is made up by some additional operators and a filter that I&#8217;m using to replace the functionality of TOP via my use of ROW_NUMBER and the WHERE clause.</p>
<p>Which brings up the point that we&#8217;re always supposed to be using the parens with TOP and I just found that out this morning while reading Books Online.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rick Kierner</title>
		<link>http://facility9.com/2009/01/26/retrieve-the-top-x-rows-from-a-query#comment-182</link>
		<dc:creator>Rick Kierner</dc:creator>
		<pubDate>Tue, 27 Jan 2009 12:14:21 +0000</pubDate>
		<guid isPermaLink="false">http://facility9.com/?p=289#comment-182</guid>
		<description>I just found this:
DECLARE @Count int
SELECT @Count = 5

SELECT TOP (@Count),
         c.ContactId,
         c.FirstName,
         c.LastName,
         e.EmployeeId,
         a.AddressId,
         a.AddressLine1,
         a.AddressLine2,
         a.City,
         a.PostalCode,
         at.Name AS AddressType
    FROM HumanResources.Employee AS e
          INNER JOIN Person.Contact AS c
            ON e.ContactId = c.ContactId
          INNER JOIN HumanResources.EmployeeAddreess AS ea
            ON e.EmployeeId = ea.EmployeeId
          INNER JOIN Person.Address AS a
            ON ea.AddressId = a.AddressId
          INNER JOIN Person.AddressType AS at
            ON a.AddressTypeId = at.AddressTypeId

Is this better/worse/same?</description>
		<content:encoded><![CDATA[<p>I just found this:<br />
DECLARE @Count int<br />
SELECT @Count = 5</p>
<p>SELECT TOP (@Count),<br />
         c.ContactId,<br />
         c.FirstName,<br />
         c.LastName,<br />
         e.EmployeeId,<br />
         a.AddressId,<br />
         a.AddressLine1,<br />
         a.AddressLine2,<br />
         a.City,<br />
         a.PostalCode,<br />
         at.Name AS AddressType<br />
    FROM HumanResources.Employee AS e<br />
          INNER JOIN Person.Contact AS c<br />
            ON e.ContactId = c.ContactId<br />
          INNER JOIN HumanResources.EmployeeAddreess AS ea<br />
            ON e.EmployeeId = ea.EmployeeId<br />
          INNER JOIN Person.Address AS a<br />
            ON ea.AddressId = a.AddressId<br />
          INNER JOIN Person.AddressType AS at<br />
            ON a.AddressTypeId = at.AddressTypeId</p>
<p>Is this better/worse/same?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Denis Gobo</title>
		<link>http://facility9.com/2009/01/26/retrieve-the-top-x-rows-from-a-query#comment-181</link>
		<dc:creator>Denis Gobo</dc:creator>
		<pubDate>Mon, 26 Jan 2009 21:29:24 +0000</pubDate>
		<guid isPermaLink="false">http://facility9.com/?p=289#comment-181</guid>
		<description>you can also use SET ROWCOUNT n, still works in 2008 but will be deprecated in the next version I believe, make sure that it is set to 0 after the query


declare @id int
select @id = 5


set rowcount @id

select * from sysobjects
order by newid()

set rowcount 0</description>
		<content:encoded><![CDATA[<p>you can also use SET ROWCOUNT n, still works in 2008 but will be deprecated in the next version I believe, make sure that it is set to 0 after the query</p>
<p>declare @id int<br />
select @id = 5</p>
<p>set rowcount @id</p>
<p>select * from sysobjects<br />
order by newid()</p>
<p>set rowcount 0</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rick Kierner</title>
		<link>http://facility9.com/2009/01/26/retrieve-the-top-x-rows-from-a-query#comment-172</link>
		<dc:creator>Rick Kierner</dc:creator>
		<pubDate>Mon, 26 Jan 2009 13:26:29 +0000</pubDate>
		<guid isPermaLink="false">http://facility9.com/?p=289#comment-172</guid>
		<description>And thanks for your help.</description>
		<content:encoded><![CDATA[<p>And thanks for your help.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
