本文介绍了在 SELECT INTO 中保留 ORDER BY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 T-SQL 查询,它从一个表中获取数据并将其复制到一个新表中,但只有满足特定条件的行:

I have a T-SQL query that takes data from one table and copies it into a new table but only rows meeting a certain condition:

SELECT VibeFGEvents.*
INTO VibeFGEventsAfterStudyStart
FROM VibeFGEvents
LEFT OUTER JOIN VibeFGEventsStudyStart
ON
    CHARINDEX(REPLACE(REPLACE(REPLACE(logName, 'MyVibe ', ''), ' new laptop', ''), ' old laptop', ''), excelFilename) > 0
    AND VibeFGEventsStudyStart.MIN_TitleInstID <= VibeFGEvents.TitleInstID
    AND VibeFGEventsStudyStart.MIN_WinInstId <= VibeFGEvents.WndInstID
WHERE VibeFGEventsStudyStart.excelFilename IS NOT NULL
ORDER BY VibeFGEvents.id

使用表格的代码依赖于它的顺序,上面的副本没有保留我预期的顺序.IE.新表 VibeFGEventsAfterStudyStart 中的行在从 VibeFGEvents.id 复制的 VibeFGEventsAfterStudyStart.id 列中不是单调递增的.

The code using the table relies on its order, and the copy above does not preserve the order I expected. I.e. the rows in the new table VibeFGEventsAfterStudyStart are not monotonically increasing in the VibeFGEventsAfterStudyStart.id column copied from VibeFGEvents.id.

在 T-SQL 中,如何在 VibeFGEventsStudyStart 中保留 VibeFGEvents 中的行顺序?

In T-SQL how might I preserve the ordering of the rows from VibeFGEvents in VibeFGEventsStudyStart?

推荐答案

有什么用?

点是——表中的数据没有排序.在 SQL Server 中,表的内在存储顺序是(如果已定义)聚集索引的存储顺序.

Point is – data in a table is not ordered. In SQL Server the intrinsic storage order of a table is that of the (if defined) clustered index.

插入数据的顺序基本上是无关紧要的".数据写入表的那一刻就忘记了.

The order in which data is inserted is basically "irrelevant". It is forgotten the moment the data is written into the table.

因此,即使您得到了这些东西,也没有任何收获.如果在处理数据时需要订单,则必须在获取它的选择上放置 order by 子句.其他任何东西都是随机的 - 即你设置数据的顺序是不确定的,可能会改变.

As such, nothing is gained, even if you get this stuff. If you need an order when dealing with data, you HAVE To put an order by clause on the select that gets it. Anything else is random - i.e. the order you et data is not determined and may change.

因此,在您尝试实现的插入上设置特定顺序是没有意义的.

So it makes no sense to have a specific order on the insert as you try to achieve.

SQL 101:集合没有顺序.

SQL 101: sets have no order.

这篇关于在 SELECT INTO 中保留 ORDER BY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 18:44
查看更多