create table ap
(id int not null,
yx varchar(30)
constraint pk_ap primary key (id)
)insert into ap
select 1,'aaa' union all
select 2,'bbb' union all
select 3,'ccc' union all
select 4,'ddd' union all
select 5,'eee'select * from apid yx
----------- ------------------------------
1 aaa
2 bbb
3 ccc
4 ddd
5 eeedeclare @r1 varchar(50),@r2 varchar(50)select @r1='',@r2=''select @r1=@r1+yx+',' from ap where id>=3
order by case when yx='yyy' then '1' else yx endselect @r2=''
select @r2=@r2+yx+',' from ap where id>=3
order by case when yx='yyy' then '1' else yx end
option(force order)select @r1 r1, @r2 r2
r1 r2
-------------------------------------------------- --------------------------------------------------
ccc,ddd,eee, eee,(1 row(s) affected)
select @r2=@r2+yx+',' from ap where id>=3
order by case when yx='yyy' then '1' else yx end
option(force order)问题在红色这里。
declare @r1 varchar(50),@r2 varchar(50)select @r1='',@r2=''select @r1=@r1+yx+',' from ap where id>=3
order by 1,--yx --case when yx='yyy' then '1' else yx endselect @r2=''
select @r2=@r2+yx+',' from ap where id>=3
order by 1,--yx --case when yx='yyy' then '1' else yx end
option(force order)select @r1 r1, @r2 r2
你都改成order by 1 或是 order by yx 然后看一下结果,应该就相同了。
order by 1select @r2=''
select @r2=@r2+yx+',' from ap where id>=3
order by 1
option(force order)select @r1 r1, @r2 r2
declare @r1 varchar(12) set @r1=''
select @r1=@r1+yx+',' from ap where id>=3 order by yx
select @r1
/*
ccc,ddd,eee,
*/declare @r1 varchar(12) set @r1=''
select @r1=@r1+yx+',' from ap where id>=3 order by yx option(force order)
select @r1
/*
ccc,ddd,eee,
*/declare @r1 varchar(12) set @r1=''
select @r1=@r1+yx+',' from ap where id>=3 order by 1
select @r1
/*
eee,
*/declare @r1 varchar(12) set @r1=''
select @r1=@r1+yx+',' from ap where id>=3 order by 1 option(force order)
select @r1
/*
eee,
*/declare @r1 varchar(12) set @r1=''
select @r1=@r1+yx+',' from ap where id>=3 order by case when yx='yyy' then '1' else yx end
select @r1/*
ccc,ddd,eee,
*/
declare @r1 varchar(12) set @r1=''
select @r1=@r1+yx+',' from ap where id>=3 order by case when yx='yyy' then '1' else yx end option(force order)
select @r1
/*
eee,
*/
-- (id int not null,
-- yx varchar(30)
-- constraint pk_ap primary key (id)
-- )-- insert into ap
-- select 1,'aaa' union all
-- select 2,'bbb' union all
-- select 3,'ccc' union all
-- select 4,'ddd' union all
-- select 5,'eee'
-- declare @r1 varchar(50),@r2 varchar(50)select @r1='',@r2=''select @r1=@r1+yx+',' from ap where id>=3
order by case when yx='yyy' then '1' else yx endselect @r2=''
select @r2=@r2+yx+',' from ap where id>=3
order by case when yx='yyy' then '1' else yx end
option(force order)select @r1 r1, @r2 r2
/*r1 r2
-------------------------------------------------- --------------------------------------------------
eee, eee,2K的表示结果一致
-- (id int not null,
-- yx varchar(30)
-- constraint pk_ap primary key (id)
-- )-- insert into ap
-- select 1,'aaa' union all
-- select 2,'bbb' union all
-- select 3,'ccc' union all
-- select 4,'ddd' union all
-- select 5,'eee'
-- declare @r1 varchar(50),@r2 varchar(50)select @r1='',@r2=''select @r1=@r1+yx+',' from ap where id>=3
select @r2=@r2+yx+',' from ap where id>=3 order by yx
select @r1 r1, @r2 r2
/*r1 r2
-------------------------------------------------- --------------------------------------------------
ccc,ddd,eee, eee,(所影响的行数为 1 行)但你的跟这个的结果却相同的。这是加ORDER BY 与没加ORDER BY 累加字符差别。
由于 SQL Server 查询优化器通常会为查询选择最佳执行计划,因此我们建议资深开发人员和数据库管理员只有在不得已时才可使用提示。
不是蛮理解 ..