简体   繁体   English

SQL选择记录创建日期之前的记录顺序

[英]SQL Select record order by the date the record was created

I have A Legacy system that runs on a SQL Server 2005. 我有一个在SQL Server 2005上运行的旧版系统。

Current table only have date column YYYYMMDD, it lacks for both a running sequence number and time. 当前表只有日期列YYYYMMDD,缺少运行序列号和时间。

Is it possible to select the a table and order by chronological? 是否可以按时间顺序选择表格和顺序? or in other words, sort the return result with records are sorted base on the date and time the record was created. 或者换句话说,对返回结果和记录进行排序,并根据记录的创建日期和时间进行排序。

If you do not have any valid time information or other information - like a vector autonumbering (IDENTITY), which was not modified - then the general answer is NO 如果您没有任何有效的时间信息或其他信息(例如未修改的向量自动编号(IDENTITY)),那么通常的回答是“ 否”

BUT

The devil is in the details - it depends on database engine you use. 细节在于细节-它取决于您使用的数据库引擎。

Just so that it might help someone else out. 只是这样可以帮助其他人。 (I realize that that OP mentions MSSQL 2005) (我意识到OP提到了MSSQL 2005)

In SQL 2008 and higher, you can use the %%physloc%% virtual column for this. 在SQL 2008和更高版本中,可以%%physloc%%使用%%physloc%%虚拟列。 This is undocumented and unsupported. 这是未记录且不受支持的。 However, it has proven to be helpful in many cases. 但是,事实证明,它在许多情况下都是有帮助的。

The hexadecimal values in %%physloc%% not only indicate the file number and page number; %%physloc%%的十六进制值不仅指示文件号和页码,还指示文件号和页码。 they also specify the slot number within the page. 他们还指定页面内的插槽号。 However, the encoding is not really human readable. 但是,编码并不是真正可读的。

The following functions can be used to make sense of the values in this virtual column. 可以使用以下功能来理解此虚拟列中的值。

1. The sys.fn_PhysLocFormatter function 1. sys.fn_PhysLocFormatter函数

This function turns the hex value returned by %%physloc%% into a nice human-readable format. 此函数将%%physloc%%返回的十六进制值转换为易于阅读的格式。 You can use it like this: 您可以像这样使用它:

SELECT *,sys.fn_PhysLocFormatter(%%physloc%%) AS PLF
  FROM dbo.tst AS T
ORDER BY PLF;

The output of this function has the format (FileNumber:PageNumber:SlotNumer) . 此函数的输出格式为(FileNumber:PageNumber:SlotNumer) Below is the output of above query: 以下是上述查询的输出:

sys.fn_PhysLocFormatter

2. The sys.fn_PhysLocCracker function 2. sys.fn_PhysLocCracker函数

This is a table valued function, you have to use the CROSS APPLY statement to call it within a query: 这是一个表值函数,您必须使用CROSS APPLY语句在查询中调用它:

SELECT * FROM dbo.tst AS T
CROSS APPLY sys.fn_PhysLocCracker(%%physloc%%) AS FPLC
ORDER BY FPLC.file_id, FPLC.page_id, FPLC.slot_id;

It returns three columns: file_id , page_id and slot_id . 它返回三列: file_idpage_idslot_id Used with the same table you have seen in the previous example, the output looks like this: 与您在上一个示例中看到的同一张表一起使用时,输出如下所示:

sys.fn_PhysLocCracker


References: 参考文献:

Where are my Rows? 我的行在哪里? – Using the %%physloc%% Virtual Column –使用%% physloc %%虚拟列

SQL Server - Find Physical Location of Records SQL Server-查找记录的物理位置

SQL Server 2008: New (undocumented) physical row locator function SQL Server 2008:新的(未记录)物理行定位器功能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM