作为存储过程中分页结果集的输出的总记录计数
我有一个关于存储过程的问题。
我尝试获取结果集的一页以及整个结果集的记录数。
其中每一个都在单独工作,但我无法将其组合起来:(
ALTER PROCEDURE dbo.pagingSCP
@PageStart INT,
@PageSize INT,
@RecordCount INT OUTPUT
AS
BEGIN
WITH AllRecords AS (
SELECT ROW_NUMBER() OVER (ORDER BY MATNR)
AS Row, viewStyleColorInModul.*
FROM viewStyleColorInModul WHERE SPRAS = 'D'
) SELECT * FROM AllRecords WHERE Row between
@PageStart and @PageStart + @PageSize
END
返回 50 行) @记录计数 = 0 @返回值=0 已完成运行 [dbo].[pagingSCP]。
ALTER PROCEDURE dbo.pagingSCP
@PageStart INT,
@PageSize INT,
@RecordCount INT OUTPUT
AS
BEGIN
WITH AllRecords AS (
SELECT ROW_NUMBER() OVER (ORDER BY MATNR)
AS Row, viewStyleColorInModul.*
FROM viewStyleColorInModul WHERE SPRAS = 'D'
) SELECT @RecordCount = Count(*) From AllRecords
END
没有行受到影响。 (返回 0 行) @RecordCount = 43770 @返回值=0 已完成运行 [dbo].[pagingSCP]。
现在可以通过某种方式获取 50 行和总记录数 在单个查询中?
提前致谢。
i have a question on stored procedures.
I try to get a page of result set and the record count of the whole set.
Each of this is working on it's own, but I'm unable to combine it:
ALTER PROCEDURE dbo.pagingSCP
@PageStart INT,
@PageSize INT,
@RecordCount INT OUTPUT
AS
BEGIN
WITH AllRecords AS (
SELECT ROW_NUMBER() OVER (ORDER BY MATNR)
AS Row, viewStyleColorInModul.*
FROM viewStyleColorInModul WHERE SPRAS = 'D'
) SELECT * FROM AllRecords WHERE Row between
@PageStart and @PageStart + @PageSize
END
(50 row(s) returned)
@RecordCount = 0
@RETURN_VALUE = 0
Finished running [dbo].[pagingSCP].
ALTER PROCEDURE dbo.pagingSCP
@PageStart INT,
@PageSize INT,
@RecordCount INT OUTPUT
AS
BEGIN
WITH AllRecords AS (
SELECT ROW_NUMBER() OVER (ORDER BY MATNR)
AS Row, viewStyleColorInModul.*
FROM viewStyleColorInModul WHERE SPRAS = 'D'
) SELECT @RecordCount = Count(*) From AllRecords
END
No rows affected.
(0 row(s) returned)
@RecordCount = 43770
@RETURN_VALUE = 0
Finished running [dbo].[pagingSCP].
Is is now somehow possible to get the 50 Rows and the total Recordcount
within the single query?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有两个不同的查询,因此您运行两个不同的 SELECT 并让 SQL 优化器单独优化每个查询。即使尝试在一个 SELECT 中获取两个查询是可能的,但也会适得其反并且不是最佳的。
附带说明一下,在客户端代码中,任何输出参数仅在迭代所有返回结果之后才可用。
You have two distinct queries, therefor eyou run two distinct SELECT and let the SQL optimizer optimize each individually. Even if trying to get both queries in one SELECT is possible, is highly counterproductive and sub-optimal.
As a side note, in the client code any output parameter is available only after iterating all results returned.
这是我们一直使用的分页过程的核心。它的工作原理是首先将所有匹配的记录转储到临时表中(WHERE SPRAS = 'D')。
然后,它从临时表中仅选择第 X 页或第 Y 页中的记录。它还包括原始选择的总记录(WHERE SPRAS = 'D')。
Here is the guts of a paging proc we used all the time. It works by first dumping all the matching records into a temp table (WHERE SPRAS = 'D').
It then selects from the temp table, only the records from page X of Y. It also includes the total records of the original selection (WHERE SPRAS = 'D').