dot*_*joe 4 sql t-sql sql-server
考虑以下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;
Run Code Online (Sandbox Code Playgroud)
我已经在mssql 2000和2005上尝试了它,并且在按顺序使用函数时都不会连接字符串.在2005年,for xml路径('')确实有效.输出是......
bar
blah
foo
barblahfoo
foo --nothing concatenated?
barblahfoo
Run Code Online (Sandbox Code Playgroud)
我无法找到记录的位置.有人可以解释为什么这不起作用?
编辑:
这是实际的执行计划.显然,排序和计算标量的顺序不一样......
alt text http://i41.tinypic.com/2d6pht3.jpg alt text http://i41.tinypic.com/w2og48.png
| 归档时间: |
|
| 查看次数: |
2696 次 |
| 最近记录: |