日期:2014-05-17  浏览次数:20421 次

关于存储过程
像如下描述的存储过程不知道如何实现:

传入三个参数,参数1:SQLstr表示查询的语句;参数2:表示每页的大小,参数3:表示要查询的第几页。
返回第几页的记录集。
SQLstr可能是复合的查询语句,如"select * from (select * from table1 where [id]>100) as A" ,"select * from table1 unit All select * from table2" 等等。

查询得的每页的记录集与其他已经查过的没有交集。

------解决方案--------------------

create proc [存储过程名]
(@p1 varchar(6000),
 @p2 int,
 @p3 int )
as
begin
 set nocount on
 declare @tsql varchar(6000)
 
 select @tsql='with t as '
             +'(select *, row_number() over(order by getdate()) ''rn'' '
             +' from ('+@p1+') y) '
             +'select top '+rtrim(@p2)+' * '
             +'from t '
             +'where rn>='+rtrim(@p2*@p3)
 
 exec(@tsql)
end