您的位置:首页 > 数据库

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: