使用 Start 和 Length 参数对 SQL 2000 进行分页的好方法是什么?

发布于 2024-07-11 17:51:44 字数 487 浏览 7 评论 0原文

我的任务是重构现有的存储过程,以便对结果进行分页。 SQL服务器是SQL 2000,所以我不能使用ROW_NUMBER分页方法。 存储过程已经相当复杂,在执行 sp_executesql 之前将大型 sql 语句的块构建在一起,并且具有各种可用的排序选项。

Google 的第一个结果似乎是一个很好的方法,但我认为这个例子错误在于第二次排序需要反转,并且开始小于页面长度的情况会崩溃。 该页面上的第二个示例似乎也是一个好方法,但 SP 使用的是 pageNumber 而不是起始记录。 整个临时表似乎会消耗性能。

我沿着这条道路取得了进展,但它看起来缓慢且令人困惑,我必须在排序顺序上执行大量 REPLACE 方法才能使其正确显示。

我还缺少其他更简单的技术吗?

I have been given the task of refactoring an existing stored procedure so that the results are paginated. The SQL server is SQL 2000 so I can't use the ROW_NUMBER method of pagination. The Stored proc is already fairly complex, building chunks of a large sql statement together before doing an sp_executesql and has various sorting options available.

The first result out of google seems like a good method but I think the example is wrong in that the 2nd sort needs to be reversed and the case when the start is less than the pagelength breaks down. The 2nd example on that page also seems like a good method but the SP is taking a pageNumber rather than the start record. And the whole temp table thing seems like it would be a performance drain.

I am making progress going down this path but it seems slow and confusing and I am having to do quite a bit of REPLACE methods on the Sort order to get it to come out right.

Are there any other easier techniques I am missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

琉璃梦幻 2024-07-18 17:51:44

不,恐怕不是 - SQL Server 2000 没有任何 2005 年的细节,如通用表表达式 (CTE) 等......Google 链接中描述的方法似乎是一种方法。

马克

No, I'm afraid not - SQL Server 2000 doesn't have any of the 2005 niceties like Common Table Expression (CTE) and such..... the method described in the Google link seems to be one way to go.

Marc

耀眼的星火 2024-07-18 17:51:44

多年来我们一直在使用此查询的变体。 此示例给出的项目为 50,000 到 50,300。

select top 300 
  Items.*
from Items
where 
  Items.CustomerId = 1234 AND
  Items.Active = 1 AND
  Items.Id not in
  ( 
    select top 50000 Items.Id
    from Items
    where 
      Items.CustomerId = 1234 AND
      Items.Active = 1
    order by Items.id
  )
order by Items.Id

We've been using variations on this query for a number of years. This example gives items 50,000 to 50,300.

select top 300 
  Items.*
from Items
where 
  Items.CustomerId = 1234 AND
  Items.Active = 1 AND
  Items.Id not in
  ( 
    select top 50000 Items.Id
    from Items
    where 
      Items.CustomerId = 1234 AND
      Items.Active = 1
    order by Items.id
  )
order by Items.Id
听你说爱我 2024-07-18 17:51:44

There are two SQL Server 2000 compliant answers in this StackOverflow question - skip the accepted one, which is 2005-only:

无畏 2024-07-18 17:51:44

Also take a look here
http://databases.aspfaq.com/database/how-do-i-page-through-a-recordset.html

scroll down to Stored Procedure Methods

亚希 2024-07-18 17:51:44

根据您的应用程序架构(以及您的数据量、结构、数据库服务器负载等),您可以使用数据库访问层进行分页。

例如,使用 ADO,您可以在记录集(ADO.NET 中的 DataSet)对象上定义页面大小,并在客户端上进行分页。 经典 ADO 甚至允许您使用服务器端游标,尽管我不知道它是否可以很好地扩展(我认为这在 ADO.NET 中已被完全删除)。

MSDN 文档:分页查询结果 (ADO.NET)

Depending on your application architecture (and your amount of data, it's structure, DB server load etc.) you could use the DB access layer for paging.

For example, with ADO you can define a page size on the record set (DataSet in ADO.NET) object and do the paging on the client. Classic ADO even lets you use a server side cursor, though I don't know if that scales well (I think this was removed altogether in ADO.NET).

MSDN documentation: Paging Through a Query Result (ADO.NET)

南烟 2024-07-18 17:51:44

在玩了一段时间之后,似乎只有一种方法可以真正做到这一点(使用 Start 和 Length 参数),那就是使用临时表。

我的最终解决方案是使用@start参数,而是使用@page参数,然后使用

    SET @sql = @sql + N'

    SELECT * FROM
    (
    SELECT TOP ' + Cast( @length as varchar) + N' * FROM 
            (
            SELECT TOP ' + Cast( @page*@length as varchar) + N'
                 field1,
                 field2 
                 From Table1
                 order by field1 ASC
            )  as Result   
            Order by Field1 DESC
     )  as Result
     Order by Field 1 ASC'

原始查询比此处显示的复杂得多,并且 order by 至少按顺序排序3 个字段,由很长的 CASE 子句确定,要求我使用一系列 REPLACE 函数来按正确的顺序获取字段。

After playing with this for a while there seems to be only one way of really doing this (using Start and Length parameters) and that's with the temp table.

My final solution was to not use the @start parameter and instead use a @page parameter and then use the

    SET @sql = @sql + N'

    SELECT * FROM
    (
    SELECT TOP ' + Cast( @length as varchar) + N' * FROM 
            (
            SELECT TOP ' + Cast( @page*@length as varchar) + N'
                 field1,
                 field2 
                 From Table1
                 order by field1 ASC
            )  as Result   
            Order by Field1 DESC
     )  as Result
     Order by Field 1 ASC'

The original query was much more complex than what is shown here and the order by was ordered on at least 3 fields and determined by a long CASE clause, requiring me to use a series of REPLACE functions to get the fields in the right order.

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