加菲猫

博客园 首页 新随笔 联系 订阅 管理

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

posted on 2008-06-10 17:33  bjh  阅读(900)  评论(0编辑  收藏  举报