简体   繁体   English

使用现有存储过程进行分页和排序

[英]Paging & sorting with an existing stored procedure

I have a legacy stored procedure that returns a large resultset. 我有一个旧的存储过程返回一个大结果集。 The problem is that the resultset is too large to get all at once and then do paging and sorting on the web side. 问题在于结果集太大,无法一次全部获取,然后在Web端进行分页和排序。

Without changing the stored procedure is it possible to wrap it in some other sql code to get only certain results back in a fast manner. 在不更改存储过程的情况下,可以将其包装在其他一些sql代码中,从而以快速的方式仅返回某些结果。

Something like: 就像是:

EXEC the_sp LIMIT 50 ORDER BY A 执行the_sp LIMIT 50 ORDER BY A

Without altering the stored procedure code your options are pretty limited. 在不更改存储过程代码的情况下,您的选择非常有限。

You could do 你可以做

CREATE TABLE #results(
    [rownum] [int] identity(1,1) NOT NULL,
    ...Other Columns matching stored procedure result format
) 

insert into #results 
EXECUTE dbo.the_sp

SELECT * FROM #results 
WHERE rownum BETWEEN 50 AND 100

Obviously it will still end up doing the same amount of work in the stored proc itself (and add some overhead for the temp table step) but it will reduce the amount of results going back to the client. 显然,它仍将在存储的proc本身中完成相同的工作量(并为临时表步骤增加一些开销),但它将减少返回给客户端的结果量。

To mitigate this, and dependant on the stored procedure logic, you might be able to use 为了减轻这种情况,并且取决于存储过程的逻辑,您也许可以使用

SET ROWCOUNT 100
EXECUTE dbo.the_sp

To stop it returning unneeded results belonging to pages after the one being displayed 要停止它,返回显示页面后不需要的结果,该结果属于页面

But as this will apply to every statement in the stored proc (not just the final one returning results to the client) this may well not be suitable. 但是,因为这将适用于存储的proc中的每条语句(而不仅仅是返回给客户端的最后一条语句),所以这可能不适合。

请参阅: SQL Server 2005分页–圣杯 (需要免费注册)(并且适用于SQL Server 2005及更高版本)

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

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