分页存储过程 + In 表达式列表使用

ALTER PROCEDURE Pw_GetGoodsInfo
@PageIndex int,  --当前页
@PageSize int,  --每页记录条数
    @CoalClassID varchar(4000), --原煤分类id
@RecordCount int out,   --返回参数,根据查询条件总共能得到的记录条数
@PageCount int out  --返回参数,根据查询条件以及分页条件总共需要分的页数
AS
BEGIN
SET NOCOUNT ON;
declare @pos int
declare @oldPos int
declare @tempstr varchar(100)

create table #temp_id
(
id int
)
set @pos=1
set @oldPos=1
while @pos<len(@CoalClassID)
begin
set @pos=charindex(',',@CoalClassID,@oldpos)
if @pos>0
begin
set @tempstr=substring(@CoalClassID,@oldpos,@pos-@oldpos)
set @oldpos=@pos+1
end
else
begin
set @tempstr=substring(@CoalClassID,@oldpos,len(@CoalClassID)-@oldpos+1)
set @pos=len(@CoalClassID)
end
insert into #temp_id
select @tempstr
end
     --审明一条sql语句
     Declare @SQLSTR nvarchar(4000)
     --计算TopCount记录数(这里声明,在下面计算)
     Declare @TopCount int
      --获得记录条数
     if   @CoalClassID = null    ---默认的情况下,没有指定分类
     begin
set @RecordCount=(select count(*) from wl_GoodsKu)
        
            --计算@PageCount,总共需要分的页数,CEILING 将一个带有小数位的数取整为下一个大于它的整数。
set @PageCount= CEILING(@RecordCount * 1.0 / @PageSize)
        
if @PageIndex=0 or @PageCount<=1 --判断如果只需要显示第一页
begin
    --如果只有一页的话就不用嵌套top分页,直接用一条简单SQL可提高效率
       set @SQLSTR=''
    set @SQLSTR=N'select top ' + str(@PageSize) + ' wl_GoodsKu.*,wl_GoodType.ClassName '
    set @SQLSTR=@SQLSTR+' from  wl_GoodsKu,wl_GoodType'
    set @SQLSTR=@SQLSTR+' where wl_GoodsKu.BigClassID=wl_GoodType.ClassID order by wl_GoodsKu.id desc'
end
            else if @PageIndex >= @PageCount -1       --当页索引超出页范围时候,或者是最后一页的时候
            begin
    set @PageIndex=@PageCount - 1
    --计算TopCount值
    set @TopCount = @RecordCount - @PageSize * @PageIndex
    --判断是最后一页时,直接用反序排一下。
                set  @SQLSTR=''
    set @SQLSTR=N'select top ' + str(@TopCount) + ' wl_GoodsKu.*,wl_GoodType.ClassName '
    set @SQLSTR=@SQLSTR+' from  wl_GoodsKu,wl_GoodType'
    set @SQLSTR=@SQLSTR+' where wl_GoodsKu.BigClassID=wl_GoodType.ClassID order by wl_GoodsKu.id asc'
end
else
begin
--除了首页和尾页的通常情况
--计算TopCount值
    set @TopCount = @RecordCount - @PageSize * @PageIndex
                set  @SQLSTR=''
    set @SQLSTR=N'select top ' + str(@PageSize) + ' * '
    set @SQLSTR=@SQLSTR+'  from (select top ' + str(@TopCount) + '  wl_GoodsKu.*,wl_GoodType.ClassName  from  wl_GoodsKu,wl_GoodType '
    set @SQLSTR=@SQLSTR+'  where wl_GoodsKu.BigClassID=wl_GoodType.ClassID order by wl_GoodsKu.id asc) as tab order by tab.id Desc'
end
    end
    else   --指定分类id的时候
    begin
set @RecordCount=(select count(*) from wl_GoodsKu where wl_GoodsKu.BigClassID in (select id from #temp_id where id is not null))
        
            --计算@PageCount,总共需要分的页数,CEILING 将一个带有小数位的数取整为下一个大于它的整数。
set @PageCount= CEILING(@RecordCount * 1.0 / @PageSize)
        
if @PageIndex=0 or @PageCount<=1 --判断如果只需要显示第一页
begin
    --如果只有一页的话就不用嵌套top分页,直接用一条简单SQL可提高效率
                set  @SQLSTR=''
    set @SQLSTR=N'select top ' + str(@PageSize) + ' wl_GoodsKu.*,wl_GoodType.ClassName '
    set @SQLSTR=@SQLSTR+' from  wl_GoodsKu,wl_GoodType'
    set @SQLSTR=@SQLSTR+' where wl_GoodsKu.BigClassID=wl_GoodType.ClassID and wl_GoodsKu.BigClassID in (select id from #temp_id where id is not null) order by wl_GoodsKu.id desc'
end
            else if @PageIndex >= @PageCount -1       --当页索引超出页范围时候,或者是最后一页的时候
            begin
    set @PageIndex=@PageCount - 1
    --计算TopCount值
    set @TopCount = @RecordCount - @PageSize * @PageIndex
    --判断是最后一页时,直接用反序排一下。
                set  @SQLSTR=''
    set @SQLSTR=N'select top ' + str(@TopCount) + ' wl_GoodsKu.*,wl_GoodType.ClassName '
    set @SQLSTR=@SQLSTR+' from  wl_GoodsKu,wl_GoodType'
    set @SQLSTR=@SQLSTR+' where wl_GoodsKu.BigClassID=wl_GoodType.ClassID and wl_GoodsKu.BigClassID in (select id from #temp_id where id is not null) order by wl_GoodsKu.id asc'
end
else
begin
--除了首页和尾页的通常情况
--计算TopCount值
    set @TopCount = @RecordCount - @PageSize * @PageIndex
                set  @SQLSTR=''
    set @SQLSTR=N'select top ' + str(@PageSize) + ' * '
    set @SQLSTR=@SQLSTR+'  from (select top ' + str(@TopCount) + '  wl_GoodsKu.*,wl_GoodType.ClassName  from  wl_GoodsKu,wl_GoodType '
    set @SQLSTR=@SQLSTR+'  where wl_GoodsKu.BigClassID=wl_GoodType.ClassID  and wl_GoodsKu.BigClassID in (select id from #temp_id where id is not null) order by wl_GoodsKu.id asc) as tab order by tab.id Desc'
end
    end
EXEC (@SQLSTR)
drop table #Temp_id
END

写完了有些地方不完美  忘记录了 遗憾啊!

posted @ 2009-03-23 13:54  迪卡.凯恩  阅读(235)  评论(0编辑  收藏  举报