Paging SQL Server 2005 Results

前端 未结 6 2024
攒了一身酷
攒了一身酷 2020-11-28 04:53

How do I page results in SQL Server 2005?

I tried it in SQL Server 2000, but there was no reliable way to do this. I\'m now wondering if SQL Server 2005 has any buil

6条回答
  •  我在风中等你
    2020-11-28 05:07

    If you're trying to get it in one statement (the total plus the paging). You might need to explore SQL Server support for the partition by clause (windowing functions in ANSI SQL terms). In Oracle the syntax is just like the example above using row_number(), but I have also added a partition by clause to get the total number of rows included with each row returned in the paging (total rows is 1,262):

    SELECT rn, total_rows, x.OWNER, x.object_name, x.object_type
    FROM (SELECT COUNT (*) OVER (PARTITION BY owner) AS TOTAL_ROWS,
             ROW_NUMBER () OVER (ORDER BY 1) AS rn, uo.*
             FROM all_objects uo
             WHERE owner = 'CSEIS') x
    WHERE rn BETWEEN 6 AND 10
    

    Note that I have where owner = 'CSEIS' and my partition by is on owner. So the results are:

    RN  TOTAL_ROWS  OWNER   OBJECT_NAME            OBJECT_TYPE
    6   1262    CSEIS   CG$BDS_MODIFICATION_TYPES   TRIGGER
    7   1262    CSEIS   CG$AUS_MODIFICATION_TYPES   TRIGGER
    8   1262    CSEIS   CG$BDR_MODIFICATION_TYPES   TRIGGER
    9   1262    CSEIS   CG$ADS_MODIFICATION_TYPES   TRIGGER
    10  1262    CSEIS   CG$BIS_LANGUAGES            TRIGGER
    

提交回复
热议问题