q****g 发帖数: 12 | 1 我用jdbc操作 sql server下的 database时,
遇到一个奇怪的现象:比如说一个简单的语句:
SELECT id, attr
INTO tempTB
FROM TB
WHERE (...)
ORDER BY attr
返回的tempTB 有的时候根本不是按照attr排序的,
确切地说,只是部分排序。比如说如果数据范围[1,1000],
输出的结果可能是 992 993,...1000, 876,877,878,。。
我多run几次,有时候又能输出正确排序后的结果。
有人遇到过这种情况么?到底怎么回事啊?
难道是sql server安装有问题?
但是我的机器刚刚重装过,以前用这个sql server的安装文件
也装过,从来没有碰到过这种问题。 | k***e 发帖数: 12 | 2 When select from tempTB, you need to use order by attr to gurantee the
returned result is ordered. | q****g 发帖数: 12 | 3 Thanks!I've also found the reason from the website,
order by在select into 或者insert里面都是没用/unpredictable的:
Tables do not have an order. In other words, tables by definition are
logically an unordered set of rows. Using ORDER BY in a INSERT...SELECT or
SELECT...INTO does not mean that the data in the table is 'ordered'. The
order of rows which you see when you do a SELECT without an ORDER BY clause
is a undefined/arbitrary order chosen by the optimizer based on the physical
characteristics, indexes,
【在 k***e 的大作中提到】 : When select from tempTB, you need to use order by attr to gurantee the : returned result is ordered.
|
|