分頁SQLServer存儲過程

字號:

/*--用存儲過程實現(xiàn)的分頁程序
    顯示指定表、視圖、查詢結(jié)果的第x頁
    對于表中主鍵或標(biāo)識列的情況,直接從原表取數(shù)查詢,其它情況使用臨時表的方法
    如果視圖或查詢結(jié)果中有主鍵,不推薦此方法
    --鄒建 2003.09--*/
    /*--調(diào)用示例
    exec p_show '地區(qū)資料'
    exec p_show '地區(qū)資料',5,3,'地區(qū)編號,地區(qū)名稱,助記碼','地區(qū)編號'
    --*/
    /*
    因為要顧及通用性,所以對帶排序的查詢語句有一定要求.如果先排序,再出結(jié)果.就是:
    exec p_show 'select top 100 percent * from 地區(qū)資料 order by 地區(qū)名稱',5,3,'地區(qū)編號,地區(qū)名稱,助記碼','地區(qū)名稱'
    --查詢語句加上:top 100 percent //top時
    */
    if exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[p_show]') and objectproperty(id, n'isprocedure') = 1)
    drop procedure [dbo].[p_show]
    go
    create proc p_show
    @querystr nvarchar(4000), --表名、視圖名、查詢語句
    @pagesize int=10, --每頁的大小(行數(shù))
    @pagecurrent int=1, --要顯示的頁
    @fdshow nvarchar (4000)='', --要顯示的字段列表,如果查詢結(jié)果有標(biāo)識字段,需要指定此值,且不包含標(biāo)識字段
    @fdorder nvarchar (1000)='' --排序字段列表
    as
    declare @fdname nvarchar(250) --表中的主鍵或表、臨時表中的標(biāo)識列名
    ,@id1 varchar(20),@id2 varchar(20) --開始和結(jié)束的記錄號
    ,@obj_id int --對象id
    --表中有復(fù)合主鍵的處理
    declare @strfd nvarchar(2000) --復(fù)合主鍵列表
    ,@strjoin nvarchar(4000) --連接字段
    ,@strwhere nvarchar(2000) --查詢條件
    select @obj_id=object_id(@querystr)
    ,@fdshow=case isnull(@fdshow,'') when '' then ' *' else ' '+@fdshow end
    ,@fdorder=case isnull(@fdorder,'') when '' then '' else ' order by '+@fdorder end
    ,@querystr=case when @obj_id is not null then ' '+@querystr else ' ('+@querystr+') a' end
    --如果顯示第一頁,可以直接用top來完成
    if @pagecurrent=1
    begin
    select @id1=cast(@pagesize as varchar(20))
    exec('select top '+@id1+@fdshow+' from '+@querystr+@fdorder)
    return
    end
    --如果是表,則檢查表中是否有標(biāo)識更或主鍵
    if @obj_id is not null and objectproperty(@obj_id,'istable')=1
    begin
    select @id1=cast(@pagesize as varchar(20))
    ,@id2=cast((@pagecurrent-1)*@pagesize as varchar(20))
    select @fdname=name from syscolumns where id=@obj_id and status=0x80
    if @@rowcount=0 --如果表中無標(biāo)識列,則檢查表中是否有主鍵
    begin
    if not exists(select 1 from sysobjects where parent_obj=@obj_id and xtype='pk')
    goto lbusetemp --如果表中無主鍵,則用臨時表處理
    select @fdname=name from syscolumns where id=@obj_id and colid in(
    select colid from sysindexkeys where @obj_id=id and indid in(
    select indid from sysindexes where @obj_id=id and name in(
    select name from sysobjects where xtype='pk' and parent_obj=@obj_id
    )))
    if @@rowcount>1 --檢查表中的主鍵是否為復(fù)合主鍵
    begin
    select @strfd='',@strjoin='',@strwhere=''
    select @strfd=@strfd+',['+name+']'
    ,@strjoin=@strjoin+' and a.['+name+']=b.['+name+']'
    ,@strwhere=@strwhere+' and b.['+name+'] is null'
    from syscolumns where id=@obj_id and colid in(
    select colid from sysindexkeys where @obj_id=id and indid in(
    select indid from sysindexes where @obj_id=id and name in(
    select name from sysobjects where xtype='pk' and parent_obj=@obj_id
    )))
    select @strfd=substring(@strfd,2,2000)
    ,@strjoin=substring(@strjoin,5,4000)
    ,@strwhere=substring(@strwhere,5,4000)
    goto lbusepk
    end
    end
    end
    else
    goto lbusetemp
    /*--使用標(biāo)識列或主鍵為單一字段的處理方法--*/
    lbuseidentity:
    exec('select top '+@id1+@fdshow+' from '+@querystr
    +' where '+@fdname+' not in(select top '
    +@id2+' '+@fdname+' from '+@querystr+@fdorder
    +')'+@fdorder
    )
    return
    /*--表中有復(fù)合主鍵的處理方法--*/
    lbusepk:
    exec('select '+@fdshow+' from(select top '+@id1+' a.* from
    (select top 100 percent * from '+@querystr+@fdorder+') a
    left join (select top '+@id2+' '+@strfd+'
    from '+@querystr+@fdorder+') b on '+@strjoin+'
    where '+@strwhere+') a'
    )
    return
    /*--用臨時表處理的方法--*/
    lbusetemp:
    select @fdname='[id_'+cast(newid() as varchar(40))+']'
    ,@id1=cast(@pagesize*(@pagecurrent-1) as varchar(20))
    ,@id2=cast(@pagesize*@pagecurrent-1 as varchar(20))
    exec('select '+@fdname+'=identity(int,0,1),'+@fdshow+'
    into #tb from'+@querystr+@fdorder+'
    select '+@fdshow+' from #tb where '+@fdname+' between '
    +@id1+' and '+@id2
    )
    go