SQL Server 结果集分页
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编辑1
没有 SQL Server 2000 来测试,我不记得当时表变量是否可用。这是使用临时表的过程。添加了可用于分页的 RowNum 标识列。
编辑2
在这种情况下,ProcToWrap 的输出是列 ID 和名称。 RowNum 自动生成。
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.
Edit 2
Output from ProcToWrap in this case is columns ID and Name. RowNum is generated automatically.
从 SP 获取结果并将其放入临时表中,然后您可以从该表中选择 X 个结果。
Get the results from the SP and put them in a temporary table, then you can select X results from that table.
正如其他人所说,您必须将过程的结果放入临时表中,然后从中选择所需的行。
<罢工>
要从结果中获取一组行,您需要使用
ROW_NUMER()
函数:编辑: 刚刚意识到您是使用没有
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:EDIT: Just realised you are using SQL Server 2000 which does not have
ROW_NUMBER()
, sorryEDIT2: 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