如何获取 SQL Server 表中的第 n 行? [英] How do I get the nth row in a SQL Server table?

查看:83
本文介绍了如何获取 SQL Server 表中的第 n 行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 SQL Server 中的查询结果中获取第 n 行(即第 5 行)?

How would you get the nth row (i.e 5th row) from the result of a query in SQL Server?

推荐答案

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: )

这篇关于如何获取 SQL Server 表中的第 n 行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆