1

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

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

每一个都是独立的,但我无法将它结合起来:

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 行)@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

没有行受到影响。(0 行返回)@RecordCount = 43770 @RETURN_VALUE = 0 完成运行 [dbo].[pagingSCP]。

现在是否有可能在单个查询中获得 50 行和总记录数?

提前致谢。

4

2 回答 2

3
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 中同时获取两个查询可能的,也会适得其反并且不是最理想的。

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

于 2010-11-04T15:45:29.933 回答
0

这是我们一直使用的分页过程的内容。它首先将所有匹配的记录转储到临时表中(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
于 2010-11-06T00:30:24.110 回答