简体   繁体   English

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

[英]Total RecordCount as OUTPUT of Paged Result Set in Stored Procedure

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]. (返回了50行)@RecordCount = 0 @RETURN_VALUE = 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

No rows affected. 没有受影响的行。 (0 row(s) returned) @RecordCount = 43770 @RETURN_VALUE = 0 Finished running [dbo].[pagingSCP]. (返回0行)@RecordCount = 43770 @RETURN_VALUE = 0完成运行[dbo]。[pagingSCP]。

Is is now somehow possible to get the 50 Rows and the total Recordcount within the single query? 现在可以通过某种方式在单个查询中获得50行和总Recordcount吗?

Thanks in advance. 提前致谢。

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. 您有两个不同的查询,因此您要运行两个不同的SELECT,然后让SQL优化器分别对每个查询进行优化。 Even if trying to get both queries in one SELECT is possible, is highly counterproductive and sub-optimal. 即使尝试在一个SELECT中获得两个查询都是可能的,但这种做法会适得其反,而且效果欠佳。

As a side note, in the client code any output parameter is available only after iterating all results returned. 附带说明一下,在客户端代码中,只有迭代返回的所有结果之后,任何输出参数才可用。

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'). 首先将所有匹配的记录转储到临时表中(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'). 然后,它从临时表中仅选择Y页面X中的记录。它还包括原始选择的总记录(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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM