问题描述
如何按照创建的顺序查询数据?
How can I query data in the order it was created?
此表中没有日期创建字段.
I don't have a date-created field in this table.
推荐答案
如果您没有存储插入时间的字段,或关于插入顺序的任何其他元数据,没有可靠的获取此信息的方式.
If you don't have a field storing the time of insertion, or any other meta-data regarding the order of insertion, there is no reliable way to get this information.
您可能依赖于聚集索引键,但不能保证这些.IDENTITY
字段或其他自动生成的字段都不是.
You could maybe depend on a clustered index key, but these are not guaranteed. Neither are IDENTITY
fields or other auto-generated fields.
澄清一下,IDENTITY
字段会自动递增,但是...
To clarify, an IDENTITY
field does auto-increment, but...
- 您可以使用
IDENTITY_INSERT
插入显式值 - 您可以重新播种并开始重用值
- 对于身份字段没有内置的唯一性强制执行
如果 ID 字段是您的 PK,您可能可以使用它来获得一个粗略的想法:
If the ID field is your PK, you can probably use that to get a rough idea:
SELECT *
FROM MyTable
ORDER BY IdField ASC
根据您的评论,该字段是一个 GUID
.在这种情况下,没有办法返回任何可靠的顺序,因为 GUID
本质上是随机和非顺序的.
Per your comment, the field is a GUID
. In that case, there is no way to return any sort of reliable order since GUID
s are inherently random and non-sequential.
这篇关于按插入时间对记录进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!