我有一个表table1
(id, col1, col2, col3, text
),id
是主键,并且它有一个聚集索引。SQL Server如何命令集群索引表上的SELECT语句的结果集
当我运行查询
Select * from table1
我得到的结果如下,这是预期
id col1 col2 col3 text
---------------------------------
779 027 15 001 test1
780 027 15 600 test1
781 027 15 001 test2
782 027 15 600 test2
783 027 15 001 test3
784 027 15 600 test3
,但如果我运行下面的查询
select * from table1
order by col1, col2, col3
我得到这个
id col1 col2 col3 text
---------------------------------
779 027 15 001 test1
781 027 15 001 test2
783 027 15 001 test3
784 027 15 600 test3
782 027 15 600 test2
780 027 15 600 test1
我的问题是为什么最后三条记录与前三条记录的顺序相反?
你想用'COL1,COL2命令行, col3' - 你明白了 - 对吧?这没什么错 - 你得到你要求的。任何其他(没有明确的ORDER BY'的任何进一步的“排序”)都不能保证--SQL Server可以以任何顺序返回数据(以及任何顺序最快的) –