sql 多表联合分页存储过程例子
2008-06-10 17:33
507 查看
CREATE PROCEDURE page_fjhz --分级汇总分页存储过程,返回符合条件的信息总数和记录
(
@jcxz char(1),
@jclen char(1),
@str varchar(500),
@strXM varchar(500),
@strSUM varchar(500),
@PageSize int = 10, -- 页尺寸
@PageIndex int = 1, -- 页码
@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
@strWhere varchar(500) = '' -- 查询条件 (注意: 不要加 where)
)
AS
SET NOCOUNT ON
declare @strSQL nvarchar(4000) -- 主语句
declare @tmpstrSQL varchar(5000) -- 主语句
declare @strTmp varchar(110) -- 临时变量
declare @strOrder varchar(400) -- 排序类型
declare @tmpjcstr varchar(100)
set nocount on
if @jcxz='0'--第几级
set @tmpjcstr= ' leve='+@jclen
else --前几级
set @tmpjcstr= ' leve<='+@jclen
declare @cnt int
set @strSQL ='select @cnt=count(*) from zy where '+@tmpjcstr
EXEC sp_executesql @strSQL,N'@cnt int output',@cnt output
select @cnt as 'infocount'
set @strSQL=''
if @OrderType != 0
begin
set @strTmp = '<(select min'
set @strOrder = ' order by dm desc'--如果@OrderType不是0,就执行降序,这句很重要!
end
else
begin
set @strTmp = ' >(select max'
set @strOrder = ' order by dm asc'
end
if @PageIndex = 1
begin
set @strSQL = 'select top ' + str(@PageSize) +'* from (select dm,mc,leve,isnull(sum(cc.cnt),0) as cnt,'+@strXM+' from zy left outer join (SELECT DISTINCT zydm as zydm, count(distinct xszh) as cnt,'+@strSUM+' FROM jfjl where ('+@strWhere+') GROUP BY zydm) cc on cc.zydm like zy.dm+''%'' group by dm,mc,leve) mm where '+ @tmpjcstr+ @strOrder
set @tmpstrSQL = 'select mc,cnt,'+@str+' from (select dm,mc,leve,isnull(sum(cc.cnt),0) as cnt,'+@strXM+' from zy left outer join (SELECT DISTINCT zydm as zydm, count(distinct xszh) as cnt,'+@strSUM+' FROM jfjl where ('+@strWhere+') GROUP BY zydm) cc on cc.zydm like zy.dm+''%'' group by dm,mc,leve) mm where '+ @tmpjcstr+ @strOrder
end
else
begin
set @strSQL ='select top ' + str(@PageSize) +'* from (select dm as dm,mc,leve as leve,isnull(sum(cc.cnt),0) as cnt,'+@strXM+' from zy left outer join (SELECT DISTINCT zydm as zydm, count(distinct xszh) as cnt,'+@strSUM+' FROM jfjl where ('+@strWhere+') GROUP BY zydm) cc on cc.zydm like zy.dm+''%'' group by dm,mc,leve) mm where '+ @tmpjcstr +'and dm '
+ @strTmp + '(dm) from (select top ' + str((@PageIndex-1)*@PageSize) + 'dm from zy where '+ @tmpjcstr+ @strOrder+' ) as tblTmp) '+ @strOrder
set @tmpstrSQL = 'select mc,cnt,'+@str+' from (select dm,mc,leve,isnull(sum(cc.cnt),0) as cnt,'+@strXM+' from zy left outer join (SELECT DISTINCT zydm as zydm, count(distinct xszh) as cnt,'+@strSUM+' FROM jfjl where ('+@strWhere+') GROUP BY zydm) cc on cc.zydm like zy.dm+''%'' group by dm,mc,leve) mm where '+ @tmpjcstr+ @strOrder
end
exec (@strSQL)
select @tmpstrSQL as 'sql'
--取得合计数
if @strWhere!=''
set @strSQL='select ''合计:'' as sm,count(distinct xszh) as cnt,'+@strSUM+' from jfjl where '+@strWhere
else
set @strSQL='select ''合计:'' as sm,count(distinct xszh) as cnt,'+@strSUM+' from jfjl '
select @strSQL as 'sumsql'
if @cnt>0
begin
if ( @cnt/@PageSize=@PageIndex-1) or (@cnt%@PageSize>0 and @cnt/@PageSize=@PageIndex-1) or ( @cnt/@PageSize=@PageIndex)
begin
if @strWhere!=''
set @strSQL='select count(distinct xszh) as cnt,'+@strSUM+' from jfjl where '+@strWhere
else
set @strSQL='select count(distinct xszh) as cnt,'+@strSUM+' from jfjl '
exec (@strSQL)
end
end
GO
(
@jcxz char(1),
@jclen char(1),
@str varchar(500),
@strXM varchar(500),
@strSUM varchar(500),
@PageSize int = 10, -- 页尺寸
@PageIndex int = 1, -- 页码
@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
@strWhere varchar(500) = '' -- 查询条件 (注意: 不要加 where)
)
AS
SET NOCOUNT ON
declare @strSQL nvarchar(4000) -- 主语句
declare @tmpstrSQL varchar(5000) -- 主语句
declare @strTmp varchar(110) -- 临时变量
declare @strOrder varchar(400) -- 排序类型
declare @tmpjcstr varchar(100)
set nocount on
if @jcxz='0'--第几级
set @tmpjcstr= ' leve='+@jclen
else --前几级
set @tmpjcstr= ' leve<='+@jclen
declare @cnt int
set @strSQL ='select @cnt=count(*) from zy where '+@tmpjcstr
EXEC sp_executesql @strSQL,N'@cnt int output',@cnt output
select @cnt as 'infocount'
set @strSQL=''
if @OrderType != 0
begin
set @strTmp = '<(select min'
set @strOrder = ' order by dm desc'--如果@OrderType不是0,就执行降序,这句很重要!
end
else
begin
set @strTmp = ' >(select max'
set @strOrder = ' order by dm asc'
end
if @PageIndex = 1
begin
set @strSQL = 'select top ' + str(@PageSize) +'* from (select dm,mc,leve,isnull(sum(cc.cnt),0) as cnt,'+@strXM+' from zy left outer join (SELECT DISTINCT zydm as zydm, count(distinct xszh) as cnt,'+@strSUM+' FROM jfjl where ('+@strWhere+') GROUP BY zydm) cc on cc.zydm like zy.dm+''%'' group by dm,mc,leve) mm where '+ @tmpjcstr+ @strOrder
set @tmpstrSQL = 'select mc,cnt,'+@str+' from (select dm,mc,leve,isnull(sum(cc.cnt),0) as cnt,'+@strXM+' from zy left outer join (SELECT DISTINCT zydm as zydm, count(distinct xszh) as cnt,'+@strSUM+' FROM jfjl where ('+@strWhere+') GROUP BY zydm) cc on cc.zydm like zy.dm+''%'' group by dm,mc,leve) mm where '+ @tmpjcstr+ @strOrder
end
else
begin
set @strSQL ='select top ' + str(@PageSize) +'* from (select dm as dm,mc,leve as leve,isnull(sum(cc.cnt),0) as cnt,'+@strXM+' from zy left outer join (SELECT DISTINCT zydm as zydm, count(distinct xszh) as cnt,'+@strSUM+' FROM jfjl where ('+@strWhere+') GROUP BY zydm) cc on cc.zydm like zy.dm+''%'' group by dm,mc,leve) mm where '+ @tmpjcstr +'and dm '
+ @strTmp + '(dm) from (select top ' + str((@PageIndex-1)*@PageSize) + 'dm from zy where '+ @tmpjcstr+ @strOrder+' ) as tblTmp) '+ @strOrder
set @tmpstrSQL = 'select mc,cnt,'+@str+' from (select dm,mc,leve,isnull(sum(cc.cnt),0) as cnt,'+@strXM+' from zy left outer join (SELECT DISTINCT zydm as zydm, count(distinct xszh) as cnt,'+@strSUM+' FROM jfjl where ('+@strWhere+') GROUP BY zydm) cc on cc.zydm like zy.dm+''%'' group by dm,mc,leve) mm where '+ @tmpjcstr+ @strOrder
end
exec (@strSQL)
select @tmpstrSQL as 'sql'
--取得合计数
if @strWhere!=''
set @strSQL='select ''合计:'' as sm,count(distinct xszh) as cnt,'+@strSUM+' from jfjl where '+@strWhere
else
set @strSQL='select ''合计:'' as sm,count(distinct xszh) as cnt,'+@strSUM+' from jfjl '
select @strSQL as 'sumsql'
if @cnt>0
begin
if ( @cnt/@PageSize=@PageIndex-1) or (@cnt%@PageSize>0 and @cnt/@PageSize=@PageIndex-1) or ( @cnt/@PageSize=@PageIndex)
begin
if @strWhere!=''
set @strSQL='select count(distinct xszh) as cnt,'+@strSUM+' from jfjl where '+@strWhere
else
set @strSQL='select count(distinct xszh) as cnt,'+@strSUM+' from jfjl '
exec (@strSQL)
end
end
GO
相关文章推荐
- SQL通用存储过程分页,支持多表联合
- SQL分页存储过程(不支持多表联合查询,不支持多字段排序)
- [MSSQL]SQL通用存储过程分页,支持多表联合
- [MSSQL]SQL通用存储过程分页,支持多表联合
- SQL分页存储过程及其应用
- SQL 2005通用分页存储过程
- 存储过程和SQL相结合的运用的例子。
- 针对sql 2005的分页存储过程 .
- 使用系统存储过程实现的通用分页存储过程.sql
- sql存储过程分页
- Sql存储过程分页--临时表存储
- SQL存储过程分页另一方法
- SQL存储过程分页算法研究(支持千万级)
- SQL2005 存储过程通用分页
- sql2005 分页存储过程
- sql存储过程分页(sql语句)
- 利用SQL未公开的存储过程实现分页(更加好用的一个!)
- SQL存储过程之分页用法
- SQL分页存储过程实例
- SQL分页存储过程