SQL Server 结果集分页

发布于 2024-10-12 21:14:26 字数 134 浏览 2 评论 0原文

我在 SQL Server 2000 DB 中有一个非常丰富的存储过程,它返回单个结果集。我不想(不允许)触及原始 SP,但想向返回的记录添加分页。

是否可以将此 SP 与另一个接受返回结果集并仅提供行 X 到 Y 的 SP 包装在一起?

I have a very meaty stored procedure in a SQL Server 2000 DB which returns a single resultset. I don't want to (not allowed to) touch the original SP but would like add pagination to the returned records.

Is it possible to wrap this SP with another that takes the returned resultset and only gives me rows X to Y ?

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

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

发布评论

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

评论(3

轻许诺言 2024-10-19 21:14:26
create procedure ProcWrap
as

declare @T table (ID int, Name nvarchar(50))

insert into @T
exec ProcToWrap

select *
from @T
where ID < 10

编辑1
没有 SQL Server 2000 来测试,我不记得当时表变量是否可用。这是使用临时表的过程。添加了可用于分页的 RowNum 标识列。

create procedure ProcWrap2
as

create table #T (RowNum int identity, ID int, Name nvarchar(50))

insert into #T
exec ProcToWrap

select *
from #T
where RowNum between 10 and 19

drop table #T

编辑2
在这种情况下,ProcToWrap 的输出是列 ID 和名称。 RowNum 自动生成。

create procedure ProcWrap
as

declare @T table (ID int, Name nvarchar(50))

insert into @T
exec ProcToWrap

select *
from @T
where ID < 10

Edit 1
Don't have SQL Server 2000 to test on and I don't remember if table variables where available then. Here is a procedure using a temp table instead. Added a RowNum identity column that you can use for pagination.

create procedure ProcWrap2
as

create table #T (RowNum int identity, ID int, Name nvarchar(50))

insert into #T
exec ProcToWrap

select *
from #T
where RowNum between 10 and 19

drop table #T

Edit 2
Output from ProcToWrap in this case is columns ID and Name. RowNum is generated automatically.

↙厌世 2024-10-19 21:14:26

从 SP 获取结果并将其放入临时表中,然后您可以从该表中选择 X 个结果。

Get the results from the SP and put them in a temporary table, then you can select X results from that table.

感性不性感 2024-10-19 21:14:26

正如其他人所说,您必须将过程的结果放入临时表中,然后从中选择所需的行。

<罢工>
要从结果中获取一组行,您需要使用 ROW_NUMER() 函数:

SELECT
   ROW_NUMBER() OVER (ORDER BY ID) AS row_number, *
FROM
   Your_Temp_Table
WHERE row_number BETWEEN 11 AND 20 -- For the second page of results with 10 per page.

编辑: 刚刚意识到您是使用没有ROW_NUMBER()的SQL Server 2000,抱歉

EDIT2:由于您将查询结果存储在临时表中,因此您可以添加一个递增整数字段该结果集并将其用作 ROW_NUMBER() 的模拟,以便选择您需要的行。

EDIT3:这是一篇讨论SQL Server 2000 中分页的文章的链接

As others have said you will have to put the results of the procedure in a temp table then select the rows you want from that.


To get a set of rows from your results you need to use the ROW_NUMER() function:

SELECT
   ROW_NUMBER() OVER (ORDER BY ID) AS row_number, *
FROM
   Your_Temp_Table
WHERE row_number BETWEEN 11 AND 20 -- For the second page of results with 10 per page.

EDIT: Just realised you are using SQL Server 2000 which does not have ROW_NUMBER(), sorry

EDIT2: Since you are storing the results of the query in a temp table you can add an incrementing integer field to that result set and use that as a simulation for the ROW_NUMBER() in order to select the row you need.

EDIT3: Here's a link to an article discussing pagination in SQL Server 2000

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