Consider the following tsql ...
create function dbo.wtfunc(@s varchar(50)) returns varchar(10) begin return left(@s, 2); end GO select t.* into #test from ( select 'blah' as s union select 'foo' union select 'bar' ) t select * from #test; declare @s varchar(100); set @s = ''; select @s = @s + s from #test order by s; select @s; set @s = ''; select @s = @s + s from #test order by dbo.wtfunc(s); select @s; /* 2005 only*/ select cast((select s+'' from #test order by dbo.wtfunc(s) for xml path('')) as varchar(100)) drop function dbo.wtfunc; drop table #test;
I tried this on mssql 2000 and 2005 and both do not concatenate the string when using the function in order. In 2005, the for xml ('') path works. Output signal ...
bar blah foo barblahfoo foo --nothing concatenated? barblahfoo
I can not find where this is documented. Can someone shed some light on why this is not working?
EDIT:
Here are the actual implementation plans. Obviously, sorting and computational scalar are not in the same order ...
alt text http://i41.tinypic.com/2d6pht3.jpg alt text http://i41.tinypic.com/w2og48.png
source share