且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何获取 SQL Server 表中的第 n 行?

更新时间:2023-12-01 09:48:28

SQL Server 2005 及更新版本:

SQL Server 2005 and newer:

with Records AS(select row_number() over(order by datecreated) as 'row', * 
                from Table)
select * from records
where row=5

您可以通过更改顺序来确定如何对数据进行排序以获得第五行.

You can change the order by to determine how you sort the data to get the fifth row.

在我的本地安装上测试:Microsoft SQL Server 2005 - 9.00.4053.00 (X64) 2009 年 5 月 26 日 14:13:01 版权所有 (c) 1988-2005 Microsoft Corporation Developer Edition(64 位),Windows NT 6.1(内部版本)7600:)

Tested on my local install: Microsoft SQL Server 2005 - 9.00.4053.00 (X64) May 26 2009 14:13:01 Copyright (c) 1988-2005 Microsoft Corporation Developer Edition (64-bit) on Windows NT 6.1 (Build 7600: )