网上找了很多都是对单表实现的,有没有分页存储过程可以满足下面的功能
1.对where 条件参数化
2.支持多表查询,比如select id,name from A union select id,name from B
各个版本的都有:http://www.cyqdata.com/cyqdata/article-detail-37508
多表查询就用视图。
分页存储过程加多表查询
CREATE PROC LoadPageData
@pageIndex int,
@pageSize int ,
@totalCount int output
as
select * into #Temp from(
SELECT B.*,P.ProjectName,BR.BranchName,U.UName FROM BuildingInfo AS B
LEFT JOIN ProjectInfo AS P ON B.ProjectId = P.Id
LEFT JOIN Branch AS BR ON BR.Id=P.BranchId
LEFT JOIN UserInfo AS U ON P.SubBy=U.Id) as d
--select * from #Temp
declare @str nvarchar(1000);
set @str= N'select top('+CAST(@pageSize as nvarchar(30))+') * from #Temp as T where T.Id not in (select top('+cast((@pageSize*(@pageIndex-1)) as nvarchar(30))+') Id from #Temp as P order by P.Id) order by T.Id'
print @str
exec (@str)
drop table #Temp
可以参考一下我的博客:
http://www.cnblogs.com/hanyinglong/archive/2011/12/09/wdqaq.html
http://www.cnblogs.com/aehyok/archive/2012/12/04/2800881.html你可以试试这个,好好看看其实很简单,SQL Server 自带的,你自己封装成存储过程就可以了