通用分页存储过程,源码共享,大家共同完善

好久没有上来写点东西了,今天正好有空,共享一些个人心得,就是关于分页的存储过程,这个问题应该是老生重谈了,网上的通用存储过程的类型已经够多了,但是,好象看到的基本上不能够满足一些复杂的sql语句的分页(也可能是我不够见多识广啊,呵呵),比如下面这句: 

select ” as checkbox, a.targetid, a.targetperiod, convert(varchar(10), b.begindate, 120) as begindate, 

    convert(varchar(10), b.enddate, 120) as enddate, c.salescode, c.salesname, d.catalogcode, d.catalogname, 

    e.orgid, e.orgname, f.orgid as branchorgid, f.orgcode as branchorgcode, f.orgname as branchorgname, 

    a.amount, ” as detailbutton

from chlsalestarget as a

    left outer join chlsalestargetperiod as b on a.targetperiod=b.targetperiod

    left outer join chlsales as c on a.sales=c.salescode

    left outer join chlitemcatalog as d on a.itemcatalog=d.catalogcode

    left outer join chlorg as e on a.orgid=e.orgid

    left outer join chlorg as f on c.branchorgid=f.orgid

where a.targetperiod >=’200607′ and a.targetperiod <=’200608′ and f.orgcode like ‘%123%’ and e.orgcode like ‘%123%’

order by a.targetperiod desc,c.salesname,d.catalogname上面这句sql里面有一些特殊情况,比如使用了convert函数,而且没有主键,有多表连接,有表别名,字段别名等等,这些情况处理起来可能比较棘手,当然,其中的“” as checkbox”是我系统当中的特例情况,用来做一些处理的。

    我这里提供一个自己开发的通用分页存储过程,有什么好的建议和意见,大家请不吝指教。代码如下:

通用分页存储过程—-sp_paging

/**//*

============================================================

功能:    通用分页存储过程

参数:

    @pk    varchar(50),            主键,用来排序的单一字段,空的话,表示没有主键,存储过程将自动创建标识列主键

    @fields    varchar(500),        要显示的字段列表(格式如:id,code,name)

    @tables varchar(1000),        要使用的表集合(org)

    @where varchar(500),        查询条件(code like ‘100’)

    @orderby varchar(100),        排序条件(支持多个排序字段,如:id,code desc,name desc)

    @pageindex int,                当前要显示的页的页索引,索引从1开始,无记录时为0。

    @pagesize int,                页大小

创建者:hollis yao

创建日期:2006-08-06

备注:

============================================================

*/

create procedure [dbo].[sp_paging]

    @pk    varchar(50)=”,

    @fields    varchar(500),

    @tables varchar(1000),

    @where varchar(500)=”,

    @orderby varchar(100),

    @pageindex int,

    @pagesize int

as

–替换单引号,避免构造sql出错

set @fields = replace(@fields, ””, ”””)

–要执行的sql,切分为几个字符串,避免出现长度超过4k时的问题

declare @sql1 varchar(4000)

declare @sql2 varchar(4000)

set @sql1 = ”

set @sql2 = ”

if @where is not null and len(ltrim(rtrim(@where))) > 0

    set @where = ‘ where ‘ + @where

else

    set @where = ‘ where 1=1’

set @sql1 = @sql1 + ‘ declare @totalcount int’    –声明一个变量,总记录数

set @sql1 = @sql1 + ‘ declare @pagecount int’    –声明一个变量,总页数

set @sql1 = @sql1 + ‘ declare @pageindex int’    –声明一个变量,页索引

set @sql1 = @sql1 + ‘ declare @startrow int’    –声明一个变量,当前页第一条记录的索引

set @sql1 = @sql1 + ‘ select @totalcount=count(*) from ‘ + @tables + @where    –获取总记录数

set @sql1 = @sql1 + ‘ if @pagecount <= 0 begin’    –如果记录数为0,直接输出空的结果集

set @sql1 = @sql1 + ‘ select ‘ + @fields + ‘ from ‘ + @tables + ‘ where 1<>1’

set @sql1 = @sql1 + ‘ select 0 as pageindex,0 as pagecount,’ + convert(varchar, @pagesize) + ‘ as pagesize,0 as totalcount’

set @sql1 = @sql1 + ‘ return end’

set @sql1 = @sql1 + ‘ set @pagecount=(@totalcount+’ + convert(varchar, @pagesize) + ‘-1)/’ + convert(varchar, @pagesize)    –获取总页数

set @sql1 = @sql1 + ‘ set @pageindex=’ + convert(varchar, @pageindex)    –设置正确的页索引

set @sql1 = @sql1 + ‘ if @pageindex<0 set @pageindex=1’

set @sql1 = @sql1 + ‘ if @pageindex>@pagecount and @pagecount>0 set @pageindex=@pagecount’

set @sql1 = @sql1 + ‘ set @startrow=(@pageindex-1)*’ + convert(varchar, @pagesize) + ‘+1’

if (charindex(‘,’, @orderby)=0 and charindex(@pk, @orderby)>0)

begin

    –****************************************************************************

    –****************不需要创建主键********************************************

    –****************************************************************************

    declare @sortdirection varchar(10)    –排序方向,>=:升序,<=:倒序

    set @sortdirection = ‘>=’

    if charindex(‘desc’, @orderby) > 0

        set @sortdirection = ‘<=’

    set @sql2 = @sql2 + ‘ declare @sort varchar(100)’    –声明一个变量,用来记录当前页第一条记录的排序字段值

    set @sql2 = @sql2 + ‘ set rowcount @startrow’    –设置返回记录数截止到当前页的第一条

    set @sql2 = @sql2 + ‘ select @sort=’ + @pk + ‘ from ‘ + @tables + @where + ‘ order by ‘ + @orderby    –获取当前页第一个排序字段值

    set @sql2 = @sql2 + ‘ set rowcount ‘ + convert(varchar, @pagesize)    –设置返回记录数为页大小

    set @where = @where + ‘ and ‘ + @pk + @sortdirection + ‘@sort’

    set @sql2 = @sql2 + ‘ select ‘ + @fields + ‘ from ‘ + @tables + @where + ‘ order by ‘ + @orderby    –输出最终显示结果

end

else

begin

    –****************************************************************************

    –*************需要创建自增长主键******************************************

    –****************************************************************************

    set @sql2 = @sql2 + ‘ declare @endrow int’

    set @sql2 = @sql2 + ‘ set @endrow=@pageindex*’ + convert(varchar, @pagesize)

    set @sql2 = @sql2 + ‘ set rowcount @endrow’

    set @sql2 = @sql2 + ‘ declare @pkbegin int’    –声明一个变量,开始索引

    set @sql2 = @sql2 + ‘ declare @pkend int’    –声明一个变量,结束索引

    set @sql2 = @sql2 + ‘ set @pkbegin=@startrow’

    set @sql2 = @sql2 + ‘ set @pkend=@endrow’

    –****************************************************************************

    –************对特殊字段进行转换,以便可以插入到临时表******************

    –****************************************************************************

    declare @tempfields varchar(500)

    set @tempfields=@fields

    set @tempfields = replace(@tempfields, ””’ as checkbox’, ”)

    set @tempfields = replace(@tempfields, ””’ as detailbutton’, ”)

    set @tempfields = replace(@tempfields, ””’ as radio’, ”)

    set @tempfields = ltrim(rtrim(@tempfields))

    if left(@tempfields,1)=’,’    –去除最左边的逗号

        set @tempfields = substring(@tempfields, 2, len(@tempfields))

    if right(@tempfields,1)=’,’    –去除最右边的逗号

        set @tempfields = substring(@tempfields, 1, len(@tempfields)-1)

    set @sql2 = @sql2 + ‘ select identity(int,1,1) as pk,’ + @tempfields + ‘ into #tb from ‘ + @tables + @where + ‘ order by ‘ + @orderby

    –****************************************************************************

    –********去除字段的表名前缀,当有字段有别名时,只保留字段别名*********

    –****************************************************************************

    declare @totalfields varchar(500)

    declare @tmp varchar(50)

    declare @i int

    declare @j int

    declare @ileft int –左括号的个数

    declare @iright int –右括号的个数

    set @i = 0

    set @j = 0

    set @ileft = 0

    set @iright = 0

    set @tmp = ”

    set @totalfields = ”

    while (len(@fields)>0)

    begin

        set @i = charindex(‘,’, @fields)

        –去除字段的表名前缀

        if (@i=0)

        begin

            –找不到逗号分割,即表示只剩下最后一个字段

            set @tmp = @fields

        end

        else

        begin

            set @tmp = substring(@fields, 1, @i)

        end

        set @j = charindex(‘.’, @tmp)

        if (@j>0)

            set @tmp = substring(@tmp, @j+1, len(@tmp))

        –*******当有字段有别名时,只保留字段别名*********

        –带括号的情况要单独处理,如convert(varchar(10), b.enddate, 120) as enddate

        while (charindex(‘(‘, @tmp) > 0)

        begin

            set @ileft = @ileft + 1

            set @tmp = substring(@tmp, charindex(‘(‘, @tmp)+1, len(@tmp))

        end

        while (charindex(‘)’, @tmp) > 0)

        begin

            set @iright = @iright + 1

            set @tmp = substring(@tmp, charindex(‘)’, @tmp)+1, len(@tmp))

        end

        –当括号恰好组队的时候,才能进行字段别名的处理

        if (@ileft = @iright)

        begin

            set @ileft = 0

            set @iright = 0

            –不对这几个特殊字段作处理:checkbox、detailbutton、radio

            if (charindex(‘checkbox’, @tmp) = 0 and charindex(‘detailbutton’, @tmp) = 0 and charindex(‘radio’, @tmp) = 0)

            begin

                –判断是否有别名

                if (charindex(‘as’, @tmp) > 0)–别名的第一种写法,带’as’的格式

                begin

                    set @tmp = substring(@tmp, charindex(‘as’, @tmp)+2, len(@tmp))

                end

                else

                begin

                    if (charindex(‘ ‘, @tmp) > 0)–别名的第二种写法,带空格(” “)的格式

                    begin

                        while(charindex(‘ ‘, @tmp) > 0)

                        begin

                            set @tmp = substring(@tmp, charindex(‘ ‘, @tmp)+1, len(@tmp))

                        end

                    end

                end

            end

            set @totalfields = @totalfields + @tmp

        end

        if (@i=0)

            set @fields = ”

        else

            set @fields = substring(@fields, @i+1, len(@fields))

    end

    –print @totalfields

    set @sql2 = @sql2 + ‘ select ‘ + @totalfields + ‘ from #tb where pk between @pkbegin and @pkend order by pk’    –输出最终显示结果

    set @sql2 = @sql2 + ‘ drop table #tb’

end

–输出“pageindex(页索引)、pagecount(页数)、pagesize(页大小)、totalcount(总记录数)”

set @sql2 = @sql2 + ‘ select @pageindex as pageindex,@pagecount as pagecount,’ 

                + convert(varchar, @pagesize) + ‘ as pagesize,@totalcount as totalcount’

–print @sql1 + @sql2

exec(@sql1 + @sql2)

如果使用这个通用分页存储过程的话,那么调用方法如下:

使用通用分页存储过程进行分页

/**//*

============================================================

功能:    获取销售目标,根据条件

参数:

    @usertype int,

    @orgid varchar(500),

    @targetperiodbegin nvarchar(50),

    @targetperiodend nvarchar(50),

    @branchorgcode nvarchar(50),

    @branchorgname nvarchar(50),

    @orgcode nvarchar(50),

    @orgname nvarchar(50),

    @salescode nvarchar(50),

    @salesname nvarchar(50),

    @catalogcode nvarchar(50),

    @catalogname nvarchar(50),

    @pageindex int,                当前要显示的页的页索引,索引从1开始,无记录时为0。

    @pagesize int,                页大小

创建者:hollis yao

创建日期:2006-08-11

备注:

============================================================

*/

create procedure [dbo].[getsalestargetlist] 

@usertype int,

@orgid nvarchar(500),

@targetperiodbegin nvarchar(50),

@targetperiodend nvarchar(50),

@branchorgcode nvarchar(50),

@branchorgname nvarchar(50),

@orgcode nvarchar(50),

@orgname nvarchar(50),

@salescode nvarchar(50),

@salesname nvarchar(50),

@catalogcode nvarchar(50),

@catalogname nvarchar(50),

@pageindex int,

@pagesize int

as

declare @condition nvarchar(2000)

set @condition = ”

if (@usertype<>1)

    set @condition = @condition + ‘ and a.orgid in (‘ + @orgid + ‘)’

if (len(@targetperiodbegin)>0)

    set @condition = @condition + ‘ and a.targetperiod >=”’ + @targetperiodbegin + ””

if (len(@targetperiodend)>0)

    set @condition = @condition + ‘ and a.targetperiod <=”’ + @targetperiodend + ””

if (len(@branchorgcode)>0)

    set @condition = @condition + ‘ and f.orgcode like ”%’ + @branchorgcode + ‘%”’

if (len(@branchorgname)>0)

    set @condition = @condition + ‘ and f.orgname like ”%’ + @branchorgname + ‘%”’

if (len(@orgcode)>0)

    set @condition = @condition + ‘ and e.orgcode like ”%’ + @orgcode + ‘%”’

if (len(@orgname)>0)

    set @condition = @condition + ‘ and e.orgname like ”%’ + @orgname + ‘%”’

if (len(@salescode)>0)

    set @condition = @condition + ‘ and c.salescode like ”%’ + @salescode + ‘%”’

if (len(@salesname)>0)

    set @condition = @condition + ‘ and c.salesname like ”%’ + @salesname + ‘%”’

if (len(@catalogcode)>0)

    set @condition = @condition + ‘ and d.catalogcode like ”%’ + @catalogcode + ‘%”’

if (len(@catalogname)>0)

    set @condition = @condition + ‘ and d.catalogname like ”%’ + @catalogname + ‘%”’

if (len(@condition)>0)

    set @condition = substring(@condition,5,len(@condition))

–print @condition

exec sp_paging

    n”,n”’ as checkbox, a.targetid, a.targetperiod, convert(varchar(10), b.begindate, 120) as begindate, convert(varchar(10), b.enddate, 120) as enddate,

        c.salescode, c.salesname, d.catalogcode, d.catalogname, e.orgid, e.orgname, f.orgid as branchorgid, f.orgcode as branchorgcode, f.orgname as branchorgname, a.amount, ” as detailbutton’,

    n’chlsalestarget as a

    left outer join chlsalestargetperiod as b on a.targetperiod=b.targetperiod

    left outer join chlsales as c on a.sales=c.salescode

    left outer join chlitemcatalog as d on a.itemcatalog=d.catalogcode

    left outer join chlorg as e on a.orgid=e.orgid

    left outer join chlorg as f on c.branchorgid=f.orgid’,

    @condition,

    n’a.targetperiod desc,c.salesname,d.catalogname’,

    @pageindex, @pagesize

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐