简体   繁体   English

MS SQL SERVER PAGING

[英]MS SQL SERVER PAGING

I did a query which is : 我做了一个查询,它是:

SELECT DISTINCT m.logID 
FROM Monitor_data m 
inner join Monitor_object o on (o.objID = m.domainID)
inner join Monitor_event e on (e.mainID = m.logID)
WHERE (o.name = @objName
and m.service = @service
and e.statement = @statement
and m.start >= @start
and m.end <= @end)

That allows me to get some id (VARCHAR(50)). 这允许我得到一些id(VARCHAR(50))。 But, now I want to make a pagination so I need to modify that query. 但是,现在我想制作一个分页,所以我需要修改该查询。 Unfortunately, I cannot use LIMIT and OFFSET ... I may use ROW_NUMBER but I don't know how :/ It will be great to get a result corresponding to the rows at line n to m. 不幸的是,我不能使用LIMIT和OFFSET ...我可能会使用ROW_NUMBER但我不知道如何:/获得与第n行到第m行的行相对应的结果会很棒。 Thus, I will be able to create a paging process easily. 因此,我将能够轻松地创建一个分页过程。

Can someone help me ? 有人能帮我吗 ? Thank you. 谢谢。

Try this query: 试试这个查询:

select logID from (
    SELECT DISTINCT m.logID,
                    ROW_NUMBER() over (order by m.start) rn
    FROM Monitor_data m 
    inner join Monitor_object o on (o.objID = m.domainID)
    inner join Monitor_event e on (e.mainID = m.logID)
    WHERE (o.name = @objName
           and m.service = @service
           and e.statement = @statement
           and m.start >= @start
           and m.end <= @end)
) a where rn between (m, n) --here you provide values for limits for rows to return

Above query is based on ROW_NUMBER function in SQL Server, which requires some ordering, so I assumed that m.start will provide an order (I think it start date or something :) ). 上面的查询基于SQL Server中的ROW_NUMBER函数,这需要一些排序,所以我假设m.start将提供一个订单(我认为它开始日期或其他:))。

Use @PageIndex and PageSize for paging 使用@PageIndexPageSize传呼

declare @PageIndex int=1
declare @PageSize int=10
declare @RecordCount int
SET NOCOUNT ON;
SELECT 
    ROW_NUMBER() OVER (ORDER BY m.logID ) as RowNumber, DISTINCT m.logID 
INTO #Results
FROM Monitor_data m 
    inner join Monitor_object o on (o.objID = m.domainID)
    inner join Monitor_event e on (e.mainID = m.logID)
WHERE (o.name = @objName
    and m.service = @service
    and e.statement = @statement
    and m.start >= @start
    and m.end <= @end)

SELECT @RecordCount = COUNT(*) FROM #Results

SELECT 
    *, @RecordCount as RecordCount  FROM #Results 
WHERE  
        RowNumber BETWEEN (@PageIndex -1) * @PageSize + 1 AND (((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1

DROP TABLE #Results

I think this is what you want. 我想这就是你想要的。

Select logId from 
(SELECT DISTINCT m.logID, Row_number() over (order by (select null)) as ranking 
FROM Monitor_data m 
inner join Monitor_object o on (o.objID = m.domainID)
inner join Monitor_event e on (e.mainID = m.logID)
WHERE (o.name = @objName
and m.service = @service
and e.statement = @statement
and m.start >= @start
and m.end <= @end))
where ranking between n and m

ORDER BY ****** 按******订购

OFFSET @ItemsPerPage * (@CurrentPage - 1) ROWS OFFSET @ItemsPerPage *(@ CurrentPage - 1)ROWS

FETCH NEXT @ItemsPerPage ROWS ONLY FETCH NEXT @ItemsPerPage仅限行

DECLARE @CurrentPage int = 1;
DECLARE @ItemsPerPage int = 10;

SELECT DISTINCT m.logID 
FROM Monitor_data m 
inner join Monitor_object o on (o.objID = m.domainID)
inner join Monitor_event e on (e.mainID = m.logID)
WHERE (o.name = @objName
and m.service = @service
and e.statement = @statement
and m.start >= @start
and m.end <= @end)


ORDER BY m.logID

OFFSET @ItemsPerPage * (@CurrentPage - 1) ROWS
FETCH NEXT @ItemsPerPage ROWS ONLY

paging Example 分页示例

DECLARE @myTable TABLE(Id int, Name nvarchar(50),EventDate date);

INSERT INTO @myTable(Id, Name, EventDate)
VALUES (1, 'a', '2018-01-01'),
(2, 'b', '2018-01-02'),
(3, 'c', '2018-01-03'),
(4, 'd', '2018-01-04'),
(5, 'e', '2018-01-05'),
(6, 'f', '2018-01-06');


DECLARE @CurrentPage int = 1;
DECLARE @ItemsPerPage int = 4;


SELECT * FROM @myTable
ORDER BY EventDate  DESC
OFFSET @ItemsPerPage * (@CurrentPage - 1) ROWS
FETCH NEXT @ItemsPerPage ROWS ONLY

Starting with 2012, you could use OFFSET and FETCH. 从2012开始,您可以使用OFFSET和FETCH。 Prior to that a solution is to use ROW_NUMBER. 在此之前,解决方案是使用ROW_NUMBER。 However, beware, ROW_NUMBER() approach is very slow. 但是,请注意,ROW_NUMBER()方法非常慢。 If you don't have a performance problem you can use it. 如果您没有性能问题,可以使用它。

Whatever the version is, a fast paging is done using TOP N and ordering by your desired columns and also specifying minimum value. 无论版本是什么,使用TOP N进行快速分页并按所需列排序并指定最小值。 ie: 即:

select TOP (@pageSize) * from myTable
where myKeyValue > @minValue
order by myKeyValue;

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

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