Sqlserver2008升級到sqlserver2014的過程中,採用orderby 的語句可能出現個問題,下面舉個例子:
select 1 分組 into tempdb..baizhu003
insert into tempdb..baizhu003
select 2
insert into tempdb..baizhu003
select 3
insert into tempdb..baizhu003
select 1
insert into tempdb..baizhu003
select 2
insert into tempdb..baizhu003
select 3
select * into tempdb..baizhu004 from tempdb..baizhu003 order by 分組
select * from tempdb..baizhu004
sqlserver2008運行結果:
分組 |
1 |
1 |
2 |
2 |
3 |
3 |
Sqlserver2014運行結果:
分組 |
1 |
2 |
3 |
1 |
2 |
3 |
解決辦法是給表加上聚集索引:
create clustered index idx on tempdb..baizhu004(分組)
select * from tempdb..baizhu004
結果如下
分組 |
1 |
1 |
2 |
2 |
3 |
3 |
加上聚集索引後,此表自動排序了。