<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Subqueries in BETWEEN and CASE Statements</title><link>http://blogs.msdn.com/b/craigfr/archive/2008/06/27/subqueries-in-between-and-case-statements.aspx</link><description>Consider the following query: 
 
 CREATE TABLE T1 (A INT, B1 INT, B2 INT) CREATE TABLE T2 (A INT, B INT) 
 SELECT * FROM T1 WHERE (SELECT SUM(T2.B) FROM T2 WHERE T2.A = T1.A) BETWEEN T1.B1 AND T1.B2 
 Observe that the subquery in this query only needs</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Subqueries in BETWEEN and CASE Statements</title><link>http://blogs.msdn.com/b/craigfr/archive/2008/06/27/subqueries-in-between-and-case-statements.aspx#10381958</link><pubDate>Wed, 02 Jan 2013 23:58:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10381958</guid><dc:creator>Craig Freedman</dc:creator><description>&lt;p&gt;The BETWEEN clause and CASE statements are expanded fairly early in the processing of these queries so the workarounds should be fairly safe. &amp;nbsp;However, I&amp;#39;m reluctant to make any absolute guarantee as plans can and do change based on a variety of factors.&lt;/p&gt;
&lt;p&gt;HTH&lt;/p&gt;
&lt;p&gt;Craig&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10381958" width="1" height="1"&gt;</description></item><item><title>re: Subqueries in BETWEEN and CASE Statements</title><link>http://blogs.msdn.com/b/craigfr/archive/2008/06/27/subqueries-in-between-and-case-statements.aspx#10381411</link><pubDate>Mon, 31 Dec 2012 00:38:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10381411</guid><dc:creator>Martin Smith</dc:creator><description>&lt;p&gt;Can any of these work arounds be regarded as guaranteed behaviour or is using a variable the only sure fire way to prevent re-evaluation as per the earlier Connect Item?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10381411" width="1" height="1"&gt;</description></item><item><title>re: Subqueries in BETWEEN and CASE Statements</title><link>http://blogs.msdn.com/b/craigfr/archive/2008/06/27/subqueries-in-between-and-case-statements.aspx#10087860</link><pubDate>Mon, 08 Nov 2010 22:24:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10087860</guid><dc:creator>Greig</dc:creator><description>&lt;p&gt;Very usefull blog, thank you&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10087860" width="1" height="1"&gt;</description></item><item><title>re: Subqueries in BETWEEN and CASE Statements</title><link>http://blogs.msdn.com/b/craigfr/archive/2008/06/27/subqueries-in-between-and-case-statements.aspx#8708682</link><pubDate>Tue, 08 Jul 2008 18:48:54 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8708682</guid><dc:creator>dbenoit</dc:creator><description>&lt;p&gt;Great blog with excellent information. I agree that this is &amp;quot;hard to believe&amp;quot; behavior but it is great to know about it so that we can address things appropriately. Now if only I could find a way to programatically find where in code these things would be occurring. : ) &lt;/p&gt;
&lt;p&gt;I will certainly put my vote in on the Connect site. &lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8708682" width="1" height="1"&gt;</description></item><item><title>re: Subqueries in BETWEEN and CASE Statements</title><link>http://blogs.msdn.com/b/craigfr/archive/2008/06/27/subqueries-in-between-and-case-statements.aspx#8682322</link><pubDate>Thu, 03 Jul 2008 01:04:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8682322</guid><dc:creator>Daniel Smith</dc:creator><description>&lt;p&gt;Thanks for the feedback Craig :-)&lt;/p&gt;
&lt;p&gt;I've voted for the bug on Connect, so hopefully the query optimizer can be improved in the next version.&lt;/p&gt;
&lt;p&gt;It just seems a bit crazy that the optimizer in 2000 could handle this type of thing, yet 2005 and 2008 can't. &amp;nbsp;It's even more frustrating to hear that we might have to wait for the next version after 2008, which is likely at least 3 years away!&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8682322" width="1" height="1"&gt;</description></item><item><title>re: Subqueries in BETWEEN and CASE Statements</title><link>http://blogs.msdn.com/b/craigfr/archive/2008/06/27/subqueries-in-between-and-case-statements.aspx#8681934</link><pubDate>Wed, 02 Jul 2008 21:39:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8681934</guid><dc:creator>Craig Freedman</dc:creator><description>&lt;p&gt;I forwarded your feedback to the appropriate individuals. &amp;nbsp;Nevertheless, I would still strongly recommend that you submit your feedback directly at &lt;a rel="nofollow" target="_new" href="http://connect.microsoft.com/"&gt;http://connect.microsoft.com/&lt;/a&gt;. &amp;nbsp;See also &lt;a rel="nofollow" target="_new" href="http://connect.microsoft.com/"&gt;http://connect.microsoft.com/&lt;/a&gt;SQLServer/feedback/ViewFeedback.aspx?FeedbackID=336002.&lt;/p&gt;
&lt;p&gt;Craig&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8681934" width="1" height="1"&gt;</description></item><item><title>re: Subqueries in BETWEEN and CASE Statements</title><link>http://blogs.msdn.com/b/craigfr/archive/2008/06/27/subqueries-in-between-and-case-statements.aspx#8663606</link><pubDate>Sat, 28 Jun 2008 18:04:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8663606</guid><dc:creator>Daniel Smith</dc:creator><description>&lt;p&gt;So what are the plans to address this? &amp;nbsp;This is totally unexpected behaviour.&lt;/p&gt;
&lt;p&gt;Will this be fixed in the next service pack for 2005? &amp;nbsp;In the meantime, I hope a hotfix is being worked on as a high priority.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8663606" width="1" height="1"&gt;</description></item><item><title>re: Subqueries in BETWEEN and CASE Statements</title><link>http://blogs.msdn.com/b/craigfr/archive/2008/06/27/subqueries-in-between-and-case-statements.aspx#8663324</link><pubDate>Sat, 28 Jun 2008 10:24:56 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8663324</guid><dc:creator>chojrak11</dc:creator><description>&lt;p&gt;Thanks for the article. It made me spew out amazingly rich stream of curses. I've been fighting SQL 2005 performance problems for a year or so, many of which are exactly with CASE and BETWEEN, and today you tell me it's designed to act this way...&lt;/p&gt;
&lt;p&gt;Thanks for clear explanation why things slowed down (for me at last), instead of being 30% faster, as advertised.&lt;/p&gt;
&lt;p&gt;I hope that this weird behaviour, for which I don't see any justification, is going to be eliminated in SQL 2008, and in next SP for SQL 2005.&lt;/p&gt;
&lt;p&gt;Have a nice day.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8663324" width="1" height="1"&gt;</description></item></channel></rss>