<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: SQL SERVER &#8211; Execution Plan and Results of Aggregate Concatenation Queries Depend Upon Expression Location</title>
	<atom:link href="http://blog.sqlauthority.com/2009/09/20/sql-server-execution-plan-and-results-of-aggregate-concatenation-queries-depend-upon-expression-location/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sqlauthority.com/2009/09/20/sql-server-execution-plan-and-results-of-aggregate-concatenation-queries-depend-upon-expression-location/</link>
	<description>Personal Notes of Pinal Dave</description>
	<lastBuildDate>Thu, 09 Feb 2012 19:36:10 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: jahnavi</title>
		<link>http://blog.sqlauthority.com/2009/09/20/sql-server-execution-plan-and-results-of-aggregate-concatenation-queries-depend-upon-expression-location/#comment-56254</link>
		<dc:creator><![CDATA[jahnavi]]></dc:creator>
		<pubDate>Tue, 29 Sep 2009 22:52:47 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6786#comment-56254</guid>
		<description><![CDATA[hi Pinal,
            could you please send interview questions on DTS,SSIS,SSRS,SSAS,Crystal reports please.]]></description>
		<content:encoded><![CDATA[<p>hi Pinal,<br />
            could you please send interview questions on DTS,SSIS,SSRS,SSAS,Crystal reports please.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SQL SERVER – Interesting Observation – Execution Plan and Results of Aggregate Concatenation Queries Journey to SQL Authority with Pinal Dave</title>
		<link>http://blog.sqlauthority.com/2009/09/20/sql-server-execution-plan-and-results-of-aggregate-concatenation-queries-depend-upon-expression-location/#comment-56224</link>
		<dc:creator><![CDATA[SQL SERVER – Interesting Observation – Execution Plan and Results of Aggregate Concatenation Queries Journey to SQL Authority with Pinal Dave]]></dc:creator>
		<pubDate>Tue, 29 Sep 2009 01:33:48 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6786#comment-56224</guid>
		<description><![CDATA[[...] comments that I feel like acknowledging them as blog posts. Recently, I wrote an article on SQL SERVER – Execution Plan and Results of Aggregate Concatenation Queries Depend Upon Expression ..., which is well received in [...]]]></description>
		<content:encoded><![CDATA[<p>[...] comments that I feel like acknowledging them as blog posts. Recently, I wrote an article on SQL SERVER – Execution Plan and Results of Aggregate Concatenation Queries Depend Upon Expression &#8230;, which is well received in [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bob</title>
		<link>http://blog.sqlauthority.com/2009/09/20/sql-server-execution-plan-and-results-of-aggregate-concatenation-queries-depend-upon-expression-location/#comment-56145</link>
		<dc:creator><![CDATA[Bob]]></dc:creator>
		<pubDate>Thu, 24 Sep 2009 21:55:34 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6786#comment-56145</guid>
		<description><![CDATA[Brian,

Your observation regarding TOP being after order by is correct in cases where ORDER BY is present.  I had a parenthetical caveat in there when I first drafted the comment that would have made the generalization more specific (&quot;as the standard applies to these statements&quot;), but I thought it too wordy.

It looks like you have seen my comments here:
http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/

My comments there address the specifc case of TOP and ORDER BY.

BTW, great site pinaldave!]]></description>
		<content:encoded><![CDATA[<p>Brian,</p>
<p>Your observation regarding TOP being after order by is correct in cases where ORDER BY is present.  I had a parenthetical caveat in there when I first drafted the comment that would have made the generalization more specific (&#8220;as the standard applies to these statements&#8221;), but I thought it too wordy.</p>
<p>It looks like you have seen my comments here:<br />
<a href="http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/" rel="nofollow">http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/</a></p>
<p>My comments there address the specifc case of TOP and ORDER BY.</p>
<p>BTW, great site pinaldave!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bob</title>
		<link>http://blog.sqlauthority.com/2009/09/20/sql-server-execution-plan-and-results-of-aggregate-concatenation-queries-depend-upon-expression-location/#comment-56144</link>
		<dc:creator><![CDATA[Bob]]></dc:creator>
		<pubDate>Thu, 24 Sep 2009 21:33:39 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6786#comment-56144</guid>
		<description><![CDATA[pinaldave,

On my final point, I think it&#039;s unclear/weak.  I&#039;d like to add that the most clear reason as to why you can&#039;t expect to get AB or BA, but might in fact get just A or just B, is that the construct @=@ +col is non-deterministic and therefore, for the same range of input values, is not guaranteed to produce the same result in every instance.]]></description>
		<content:encoded><![CDATA[<p>pinaldave,</p>
<p>On my final point, I think it&#8217;s unclear/weak.  I&#8217;d like to add that the most clear reason as to why you can&#8217;t expect to get AB or BA, but might in fact get just A or just B, is that the construct @=@ +col is non-deterministic and therefore, for the same range of input values, is not guaranteed to produce the same result in every instance.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian Tkatch</title>
		<link>http://blog.sqlauthority.com/2009/09/20/sql-server-execution-plan-and-results-of-aggregate-concatenation-queries-depend-upon-expression-location/#comment-56129</link>
		<dc:creator><![CDATA[Brian Tkatch]]></dc:creator>
		<pubDate>Thu, 24 Sep 2009 13:27:10 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6786#comment-56129</guid>
		<description><![CDATA[@Bob

Very nice explanation. I do have some comments.

&quot;The ORDER BY clause only pertains to the final results from an ANSI perspective (at least, for these queries). &quot;

TOP is after the ORDER BY. The help file (TOP) explains that.
SELECT...INTO ignores the ORDER BY (see ORDER BY).

SQL Server&#039;s implementation of ORDER BY is arbitrary. I just don&#039;t feel the ANSI standard is truly an answer.]]></description>
		<content:encoded><![CDATA[<p>@Bob</p>
<p>Very nice explanation. I do have some comments.</p>
<p>&#8220;The ORDER BY clause only pertains to the final results from an ANSI perspective (at least, for these queries). &#8221;</p>
<p>TOP is after the ORDER BY. The help file (TOP) explains that.<br />
SELECT&#8230;INTO ignores the ORDER BY (see ORDER BY).</p>
<p>SQL Server&#8217;s implementation of ORDER BY is arbitrary. I just don&#8217;t feel the ANSI standard is truly an answer.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: pinaldave</title>
		<link>http://blog.sqlauthority.com/2009/09/20/sql-server-execution-plan-and-results-of-aggregate-concatenation-queries-depend-upon-expression-location/#comment-56119</link>
		<dc:creator><![CDATA[pinaldave]]></dc:creator>
		<pubDate>Thu, 24 Sep 2009 08:38:56 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6786#comment-56119</guid>
		<description><![CDATA[Bob,

Very informative and interesting note. I ran your example and got similar result. You are very correct and clear in your example.

I am sure blog readers will enjoy reading this particular comments of yours and learn something new.

Kind Regards,
Pinal]]></description>
		<content:encoded><![CDATA[<p>Bob,</p>
<p>Very informative and interesting note. I ran your example and got similar result. You are very correct and clear in your example.</p>
<p>I am sure blog readers will enjoy reading this particular comments of yours and learn something new.</p>
<p>Kind Regards,<br />
Pinal</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Zafar Iqbal</title>
		<link>http://blog.sqlauthority.com/2009/09/20/sql-server-execution-plan-and-results-of-aggregate-concatenation-queries-depend-upon-expression-location/#comment-56112</link>
		<dc:creator><![CDATA[Zafar Iqbal]]></dc:creator>
		<pubDate>Thu, 24 Sep 2009 06:36:42 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6786#comment-56112</guid>
		<description><![CDATA[It&#039;s a strange behaviour of LTRIM, RTRIM  functions]]></description>
		<content:encoded><![CDATA[<p>It&#8217;s a strange behaviour of LTRIM, RTRIM  functions</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bob</title>
		<link>http://blog.sqlauthority.com/2009/09/20/sql-server-execution-plan-and-results-of-aggregate-concatenation-queries-depend-upon-expression-location/#comment-56106</link>
		<dc:creator><![CDATA[Bob]]></dc:creator>
		<pubDate>Thu, 24 Sep 2009 03:08:15 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6786#comment-56106</guid>
		<description><![CDATA[One more item to note is that if you built:

SELECT @Str0 = @Str0 + C1 FROM T1

into a table expression (i.e. VIEW, FUNCTION, FROM (SELECT)), you may get the results you want over and over until the one day where you call the view or function with the combination of syntax that triggers the unexpected behavoir.]]></description>
		<content:encoded><![CDATA[<p>One more item to note is that if you built:</p>
<p>SELECT @Str0 = @Str0 + C1 FROM T1</p>
<p>into a table expression (i.e. VIEW, FUNCTION, FROM (SELECT)), you may get the results you want over and over until the one day where you call the view or function with the combination of syntax that triggers the unexpected behavoir.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bob</title>
		<link>http://blog.sqlauthority.com/2009/09/20/sql-server-execution-plan-and-results-of-aggregate-concatenation-queries-depend-upon-expression-location/#comment-56104</link>
		<dc:creator><![CDATA[Bob]]></dc:creator>
		<pubDate>Thu, 24 Sep 2009 03:00:59 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6786#comment-56104</guid>
		<description><![CDATA[Kuldip,

When CAST is used with a datetime, it is non-deterministic, which would change the optimization path.  In this case, the implicit CAST from nchar to varchar is deterministic.  My only point being that your example would be treated differently by the optimizer, so it&#039;s probably not a similar enough case.  If you&#039;re using @date = @date + datecol over a set of rows, I think you should consider some different construct.]]></description>
		<content:encoded><![CDATA[<p>Kuldip,</p>
<p>When CAST is used with a datetime, it is non-deterministic, which would change the optimization path.  In this case, the implicit CAST from nchar to varchar is deterministic.  My only point being that your example would be treated differently by the optimizer, so it&#8217;s probably not a similar enough case.  If you&#8217;re using @date = @date + datecol over a set of rows, I think you should consider some different construct.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bob</title>
		<link>http://blog.sqlauthority.com/2009/09/20/sql-server-execution-plan-and-results-of-aggregate-concatenation-queries-depend-upon-expression-location/#comment-56103</link>
		<dc:creator><![CDATA[Bob]]></dc:creator>
		<pubDate>Thu, 24 Sep 2009 02:58:20 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6786#comment-56103</guid>
		<description><![CDATA[pinaldave,

If you want to see what&#039;s going on here, I think you need to shift your point of view from an implementation-centric view to an ANSI point of view.  ANSI does not guarantee processing order.  Figure 2 is interesting, but it will be potentially misleading if you don&#039;t understand the ANSI rule-set SQL Server operates under in most cases.  Implementation thinking can certainly be useful at times when you really need that multi-million row query to finish before the backups fire off, but in this case, it&#039;s counterproductive to understanding what is going on.

First off, your statements all return a single row, which were properly sorted according to your ORDER BY expression; which is to say that there was really no sorting to be done because only a single row is returned.  So let&#039;s forget about the ORDER BY.  Forget that the execution plan  shows 2 rows being sorted -- that&#039;s implementation specific.  A different vendor&#039;s product could have an optimizer smart enough to see that only a single row can ever be returned from this query and therefore, avoid the unnecessary sort altogether.

Secondly, you are not guaranteed that the string concatenation will occur in any particular order.  The ORDER BY clause only pertains to the final results from an ANSI perspective (at least, for these queries).  MS SQL Server is right in line with ANSI and will not guarantee you anything different.  Don&#039;t expect to get AB in every case.

Finally,  SQL works using relational sets, so the construct varchar = varchar + varchar forms a relational set that is evaluated accordingly.  I have seen this type of construct used many times and it works sometimes - until it doesn&#039;t.  This is why you get only B in one example where I suspect you wanted AB.    After thinking about it a bit, I built this example which I hope is just different enough to show you the same mechanism is at work here:
DECLARE @sum INT
SELECT @sum = 0
SELECT @sum = @sum + num FROM (
	SELECT 1 [num] UNION SELECT 2 [num]
) [a] ORDER BY (num * num)
SELECT @sum [Is it 3]

I tried to show two things here.  The first is that the ORDER BY isn&#039;t doing what you think it&#039;s doing with respect to the order of concatenation in your query.  The order here is nonsensical.  The second thing I think the example shows is that 1+2 doesn&#039;t equal 3 with this @ = @ + col construct in all cases.  It&#039;s a bad construct to use.  Remove the ORDER BY and you get 3, leave it in and you get 2.   Heck, someone else could get 1 in theory.  There&#039;s a case I see all the time where this is used, but I can&#039;t quite recall it.  I&#039;ll post it if I think of it again.]]></description>
		<content:encoded><![CDATA[<p>pinaldave,</p>
<p>If you want to see what&#8217;s going on here, I think you need to shift your point of view from an implementation-centric view to an ANSI point of view.  ANSI does not guarantee processing order.  Figure 2 is interesting, but it will be potentially misleading if you don&#8217;t understand the ANSI rule-set SQL Server operates under in most cases.  Implementation thinking can certainly be useful at times when you really need that multi-million row query to finish before the backups fire off, but in this case, it&#8217;s counterproductive to understanding what is going on.</p>
<p>First off, your statements all return a single row, which were properly sorted according to your ORDER BY expression; which is to say that there was really no sorting to be done because only a single row is returned.  So let&#8217;s forget about the ORDER BY.  Forget that the execution plan  shows 2 rows being sorted &#8212; that&#8217;s implementation specific.  A different vendor&#8217;s product could have an optimizer smart enough to see that only a single row can ever be returned from this query and therefore, avoid the unnecessary sort altogether.</p>
<p>Secondly, you are not guaranteed that the string concatenation will occur in any particular order.  The ORDER BY clause only pertains to the final results from an ANSI perspective (at least, for these queries).  MS SQL Server is right in line with ANSI and will not guarantee you anything different.  Don&#8217;t expect to get AB in every case.</p>
<p>Finally,  SQL works using relational sets, so the construct varchar = varchar + varchar forms a relational set that is evaluated accordingly.  I have seen this type of construct used many times and it works sometimes &#8211; until it doesn&#8217;t.  This is why you get only B in one example where I suspect you wanted AB.    After thinking about it a bit, I built this example which I hope is just different enough to show you the same mechanism is at work here:<br />
DECLARE @sum INT<br />
SELECT @sum = 0<br />
SELECT @sum = @sum + num FROM (<br />
	SELECT 1 [num] UNION SELECT 2 [num]<br />
) [a] ORDER BY (num * num)<br />
SELECT @sum [Is it 3]</p>
<p>I tried to show two things here.  The first is that the ORDER BY isn&#8217;t doing what you think it&#8217;s doing with respect to the order of concatenation in your query.  The order here is nonsensical.  The second thing I think the example shows is that 1+2 doesn&#8217;t equal 3 with this @ = @ + col construct in all cases.  It&#8217;s a bad construct to use.  Remove the ORDER BY and you get 3, leave it in and you get 2.   Heck, someone else could get 1 in theory.  There&#8217;s a case I see all the time where this is used, but I can&#8217;t quite recall it.  I&#8217;ll post it if I think of it again.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian Tkatch</title>
		<link>http://blog.sqlauthority.com/2009/09/20/sql-server-execution-plan-and-results-of-aggregate-concatenation-queries-depend-upon-expression-location/#comment-56060</link>
		<dc:creator><![CDATA[Brian Tkatch]]></dc:creator>
		<pubDate>Tue, 22 Sep 2009 13:49:48 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6786#comment-56060</guid>
		<description><![CDATA[Very strange.

And thanx for the link to Ward&#039;s blog!]]></description>
		<content:encoded><![CDATA[<p>Very strange.</p>
<p>And thanx for the link to Ward&#8217;s blog!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kuldip</title>
		<link>http://blog.sqlauthority.com/2009/09/20/sql-server-execution-plan-and-results-of-aggregate-concatenation-queries-depend-upon-expression-location/#comment-56050</link>
		<dc:creator><![CDATA[Kuldip]]></dc:creator>
		<pubDate>Tue, 22 Sep 2009 06:06:59 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6786#comment-56050</guid>
		<description><![CDATA[That&#039;s Strange thing about String in Sqlserver.
I use datetime many time  in the order clause with 
cast and convert function it give me proper output
every time.]]></description>
		<content:encoded><![CDATA[<p>That&#8217;s Strange thing about String in Sqlserver.<br />
I use datetime many time  in the order clause with<br />
cast and convert function it give me proper output<br />
every time.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Phillip Senn</title>
		<link>http://blog.sqlauthority.com/2009/09/20/sql-server-execution-plan-and-results-of-aggregate-concatenation-queries-depend-upon-expression-location/#comment-55993</link>
		<dc:creator><![CDATA[Phillip Senn]]></dc:creator>
		<pubDate>Sun, 20 Sep 2009 03:57:53 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6786#comment-55993</guid>
		<description><![CDATA[That is interesting.]]></description>
		<content:encoded><![CDATA[<p>That is interesting.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

