SQL Server 的 LIMIT 和 OFFSET 等效吗?
在 PostgreSQL 中,有 Limit
和 Offset
关键字,可以非常轻松地对结果集进行分页。
SQL Server 的等效语法是什么?
In PostgreSQL there is the Limit
and Offset
keywords which will allow very easy pagination of result sets.
What is the equivalent syntax for SQL Server?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
SQL Server 2012 中现在可以轻松实现此功能。
从 SQL Server 2012 开始,此功能有效。
在 SQL Server 中限制选择 11 到 20 行的偏移量:
ORDER BY
:必需OFFSET
:可选的跳过行数NEXT
:必需的跳过行数下一行参考: https ://learn.microsoft.com/en-us/sql/t-sql/queries/select-order-by-clause-transact-sql
This feature is now made easy in SQL Server 2012.
This is working from SQL Server 2012 onwards.
Limit with offset to select 11 to 20 rows in SQL Server:
ORDER BY
: requiredOFFSET
: optional number of skipped rowsNEXT
: required number of next rowsReference: https://learn.microsoft.com/en-us/sql/t-sql/queries/select-order-by-clause-transact-sql
与
LIMIT
等效的是SET ROWCOUNT
,但如果您想要通用分页,最好编写如下查询:这里的优点是偏移量和限制的参数化,以防万一您决定更改分页选项(或允许用户这样做)。
注意:
@Offset
参数应使用从一开始的索引,而不是正常的从零开始的索引。The equivalent of
LIMIT
isSET ROWCOUNT
, but if you want generic pagination it's better to write a query like this:The advantage here is the parameterization of the offset and limit in case you decide to change your paging options (or allow the user to do so).
Note: the
@Offset
parameter should use one-based indexing for this rather than the normal zero-based indexing.注意事项:
此解决方案仅适用于 SQL Server 2005 或更高版本,因为这是在实施
ROW_NUMBER()
时实现的。A note:
This solution will only work in SQL Server 2005 or above, since this was when
ROW_NUMBER()
was implemented.您可以在公共表表达式中使用 ROW_NUMBER 来实现此目的。
You can use ROW_NUMBER in a Common Table Expression to achieve this.
特别是对于 SQL-SERVER,您可以通过许多不同的方式来实现这一点。对于给定的实际示例,我们在此处采用了 Customer 表。
示例 1:使用“SET ROWCOUNT”
要返回所有行,请将 ROWCOUNT 设置为 0
>示例 2:使用“ROW_NUMBER 和 OVER”
示例 3:使用“OFFSET 和 FETCH”,但使用此“ORDER BY”是强制性的
希望这对您有帮助。
Specifically for SQL-SERVER you can achieve that in many different ways.For given real example we took Customer table here.
Example 1: With "SET ROWCOUNT"
To return all rows, set ROWCOUNT to 0
Example 2: With "ROW_NUMBER and OVER"
Example 3 : With "OFFSET and FETCH", But with this "ORDER BY" is mandatory
Hope this helps you.
对我来说,一起使用 OFFSET 和 FETCH 很慢,所以我使用了 TOP 和 OFFSET 的组合,如下所示(速度更快):
注意: 如果您在同一个查询中同时使用 TOP 和 OFFSET,例如:
然后你会得到一个错误,因此如果要一起使用 TOP 和 OFFSET,你需要用子查询将其分开。
如果您需要使用 SELECT DISTINCT,则查询如下:
注意: 将 SELECT ROW_NUMBER 与 DISTINCT 一起使用对我来说不起作用。
For me the use of OFFSET and FETCH together was slow, so I used a combination of TOP and OFFSET like this (which was faster):
Note: If you use TOP and OFFSET together in the same query like:
Then you get an error, so for use TOP and OFFSET together you need to separate it with a sub-query.
And if you need to use SELECT DISTINCT then the query is like:
Note: The use of SELECT ROW_NUMBER with DISTINCT did not work for me.
在 Aaronaught 的解决方案上添加轻微的变化,我通常参数化页码 (@PageNum) 和页面大小 (@PageSize)。这样,每个页面单击事件仅发送请求的页码以及可配置的页面大小:
Adding a slight variation on Aaronaught's solution, I typically parametrize page number (@PageNum) and page size (@PageSize). This way each page click event just sends in the requested page number along with a configurable page size:
另一个样本:
Another sample :
这里有人讲述sql 2011 中的此功能,遗憾的是他们选择了一点不同的关键字“OFFSET / FETCH”,但它不是标准的,那么好吧。
There is here someone telling about this feature in sql 2011, its sad they choose a little different keyword "OFFSET / FETCH" but its not standart then ok.
我能做的最接近的是
我猜类似于
select * from [db].[dbo].[table] LIMIT 0, 10
The closest I could make is
Which I guess similar to
select * from [db].[dbo].[table] LIMIT 0, 10
详细阐述 Somnath-Muluk 的答案只需使用:
不添加任何额外的列。
在 SQL Server 2019 中进行了测试,但我想也可以在旧版本中使用。
Elaborating the Somnath-Muluk's answer just use:
w/o adding any extra column.
Tested in SQL Server 2019, but I guess could work in older ones as well.
由于尚未提供此代码:
重要提示:
@limit
可以替换为要检索的结果数,@offset
是要跳过的结果数where
和order by
子句,如果它们超出,将提供不正确的结果 同步order by
是明确存在的Since nobody provided this code yet:
Important points:
@limit
can be replaced with number of results to retrieve,@offset
is number of results to skipwhere
andorder by
clauses, and will provide incorrect results if they are out of syncorder by
is there explicitly if that's what's needed在 SQL Server 中,您可以将 TOP 与 ROW_NUMBER() 一起使用
In SQL server you would use TOP together with ROW_NUMBER()
我假设,在 C# 表达式/LINQ 语句中,skip 和 take 生成以下 SQL 命令
I assume that, In C# Expression/LINQ statement of skip and take generating below SQL Command
因为,我测试了更多次,这个脚本更有用,每页100万条记录,100条记录,分页工作速度更快,我的电脑执行这个脚本0秒,而与mysql相比,有自己的限制和偏移量,大约4.5秒才能得到结果。
有人可能错过了 Row_Number() 总是按特定字段排序的理解。如果我们需要按顺序只定义行,则应使用:
ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
说明:
Since, I test more times this script more useful by 1 million records each page 100 records with pagination work faster my PC execute this script 0 sec while compare with mysql have own limit and offset about 4.5 sec to get the result.
Someone may miss understanding Row_Number() always sort by specific field. In case we need to define only row in sequence should use:
ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
Explain: