作为存储过程中分页结果集的输出的总记录计数

发布于 2024-09-30 18:48:40 字数 964 浏览 0 评论 0原文

我有一个关于存储过程的问题。

我尝试获取结果集的一页以及整个结果集的记录数。

其中每一个都在单独工作,但我无法将其组合起来:(

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甚是思念 2024-10-07 18:48:40
ALTER PROCEDURE dbo.pagingSCP
@PageStart INT, 
@PageSize INT, 
@RecordCount INT OUTPUT
AS
BEGIN
  -- get record count
  WITH AllRecords AS ( 
    SELECT viewStyleColorInModul.*
  FROM viewStyleColorInModul WHERE SPRAS = 'D'
  ) SELECT @RecordCount = Count(*) From AllRecords;

  -- now get the records
  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

您有两个不同的查询,因此您运行两个不同的 SELECT 并让 SQL 优化器单独优化每个查询。即使尝试在一个 SELECT 中获取两个查询是可能的,但也会适得其反并且不是最佳的。

附带说明一下,在客户端代码中,任何输出参数仅在迭代所有返回结果之后才可用。

ALTER PROCEDURE dbo.pagingSCP
@PageStart INT, 
@PageSize INT, 
@RecordCount INT OUTPUT
AS
BEGIN
  -- get record count
  WITH AllRecords AS ( 
    SELECT viewStyleColorInModul.*
  FROM viewStyleColorInModul WHERE SPRAS = 'D'
  ) SELECT @RecordCount = Count(*) From AllRecords;

  -- now get the records
  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

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.

沒落の蓅哖 2024-10-07 18:48:40

这是我们一直使用的分页过程的核心。它的工作原理是首先将所有匹配的记录转储到临时表中(WHERE SPRAS = 'D')。

然后,它从临时表中仅选择第 X 页或第 Y 页中的记录。它还包括原始选择的总记录(WHERE SPRAS = 'D')。

ALTER PROCEDURE [dbo].[spSelectTempUsers] 

@Page       int = 0,
@NumPerPage int = 1

AS

SET NOCOUNT ON

CREATE TABLE #TempData 
(
[RowId]     [int] identity(1,1) ,
[UserId]    [int]               ,
[FirstName] [varchar](50)       ,
[LastName]  [varchar](50)       ,
[Email]     [varchar](255)      ,
[SPRAS]     [varchar](36)
)

INSERT INTO #TempData 
(
[UserId]    ,
[FirstName] ,
[LastName]  ,
[Email]     ,
[SPRAS]      
)

SELECT
[UserId]    ,
[FirstName] ,
[LastName]  ,
[Email]     ,
[SPRAS]      

FROM viewStyleColorInModul
WHERE [SPRAS] = 'D'


DECLARE @Count int
DECLARE @Pages int
DECLARE @i     int
DECLARE @j     int

IF @Page < 1 SET @Page = 1
SET @Count = (SELECT COUNT(RowId) FROM #TempData)
SET @Pages = @Count / @NumPerPage
IF (@Pages * @NumPerPage) < @Count SET @Pages = @Pages + 1
IF @Page > @Pages SET @Page = @Pages
SET @i = ((@Page -1) * @NumPerPage) +1
SET @j = @Page * @NumPerPage

SELECT 

ISNULL(t1.UserId,'')    as UserId,
ISNULL(t1.FirstName,'') as FirstName   ,
ISNULL(t1.LastName,'')  as LastName       ,
ISNULL(t1.Email,'')     as Email      ,
ISNULL(t1.SPRAS,'')     as SPRAS,

@Pages as Pages,
@Count as TotalRecords

FROM #TempData t1


WHERE   t1.RowId >= @i AND t1.RowId <= @j

ORDER BY t1.RowId

DROP TABLE #TempData
SET NOCOUNT OFF

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').

ALTER PROCEDURE [dbo].[spSelectTempUsers] 

@Page       int = 0,
@NumPerPage int = 1

AS

SET NOCOUNT ON

CREATE TABLE #TempData 
(
[RowId]     [int] identity(1,1) ,
[UserId]    [int]               ,
[FirstName] [varchar](50)       ,
[LastName]  [varchar](50)       ,
[Email]     [varchar](255)      ,
[SPRAS]     [varchar](36)
)

INSERT INTO #TempData 
(
[UserId]    ,
[FirstName] ,
[LastName]  ,
[Email]     ,
[SPRAS]      
)

SELECT
[UserId]    ,
[FirstName] ,
[LastName]  ,
[Email]     ,
[SPRAS]      

FROM viewStyleColorInModul
WHERE [SPRAS] = 'D'


DECLARE @Count int
DECLARE @Pages int
DECLARE @i     int
DECLARE @j     int

IF @Page < 1 SET @Page = 1
SET @Count = (SELECT COUNT(RowId) FROM #TempData)
SET @Pages = @Count / @NumPerPage
IF (@Pages * @NumPerPage) < @Count SET @Pages = @Pages + 1
IF @Page > @Pages SET @Page = @Pages
SET @i = ((@Page -1) * @NumPerPage) +1
SET @j = @Page * @NumPerPage

SELECT 

ISNULL(t1.UserId,'')    as UserId,
ISNULL(t1.FirstName,'') as FirstName   ,
ISNULL(t1.LastName,'')  as LastName       ,
ISNULL(t1.Email,'')     as Email      ,
ISNULL(t1.SPRAS,'')     as SPRAS,

@Pages as Pages,
@Count as TotalRecords

FROM #TempData t1


WHERE   t1.RowId >= @i AND t1.RowId <= @j

ORDER BY t1.RowId

DROP TABLE #TempData
SET NOCOUNT OFF
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文