高效多表分頁儲存過程,可支援多表查詢,任意排序

2021-10-12 10:14:41 字數 3868 閱讀 7266

create procedure usp_paginglarge

@tablenames varchar(200),     --表名,可以是多個表,但不能用別名

@primarykey varchar(100),     --主鍵,可以為空,但@order為空時該值不能為空

@fields     varchar(4000),         --要取出的字段,可以是多個表的字段,可以為空,為空表示select *

@pagesize int,             --每頁記錄數

@currentpage int,         --當前頁,0表示第1頁

@filter varchar(4000) = '',     --條件,可以為空,不用填 where

@group varchar(200) = '',     --分組依據,可以為空,不用填 group by

@order varchar(200) = '',    --排序,可以為空,為空缺省按主鍵公升序排列,不用填 order by

@recordcount int output             --總記錄數,自己增加(總記錄數)

asbegin

declare @sortcolumn varchar(200)

declare @operator char(2)

declare @sorttable varchar(200)

declare @sortname varchar(200)

if @fields = ''

set @fields = '*'

if @filter = ''

set @filter = 'where 1=1'

else

set @filter = 'where ' +   @filter

if @group <>''

set @group = 'group by ' + @group

if @order <> ''

begin

declare @pos1 int, @pos2 int

set @order = replace(replace(@order, ' asc', ' asc'), ' desc', ' desc')

if charindex(' desc', @order) > 0

if charindex(' asc', @order) > 0

begin

if charindex(' desc', @order) < charindex(' asc', @order)

set @operator = '<='

else

set @operator = '>='

endelse

set @operator = '<='

else

set @operator = '>='

set @sortcolumn = replace(replace(replace(@order, ' asc', ''), ' desc', ''), ' ', '')

set @pos1 = charindex(',', @sortcolumn)

if @pos1 > 0

set @sortcolumn = substring(@sortcolumn, 1, @pos1-1)

set @pos2 = charindex('.', @sortcolumn)

if @pos2 > 0

begin

set @sorttable = substring(@sortcolumn, 1, @pos2-1)

if @pos1 > 0 

set @sortname = substring(@sortcolumn, @pos2+1, @pos1-@pos2-1)

else

set @sortname = substring(@sortcolumn, @pos2+1, len(@sortcolumn)-@pos2)

endelse

begin

set @sorttable = @tablenames

set @sortname = @sortcolumn

endend

else

begin

set @sortcolumn = @primarykey

set @sorttable = @tablenames

set @sortname = @sortcolumn

set @order = @sortcolumn

set @operator = '>='

enddeclare @type varchar(50)

declare @prec int

select @type=t.name, @prec=c.prec

from sysobjects o 

join syscolumns c on o.id=c.id

join systypes t on c.xusertype=t.xusertype

where o.name = @sorttable and c.name = @sortname

if charindex('char', @type) > 0

set @type = @type + '(' + cast(@prec as varchar) + ')'

declare @toprows int

set @toprows = @pagesize * @currentpage + 1

--print @toprows

--print @operator

exec('

declare @sortcolumnbegin ' + @type + '

set rowcount ' + @toprows + '

select @sortcolumnbegin=' + @sortcolumn + ' from   ' + @tablenames + ' ' + @filter + ' ' + @group + ' order by ' + @order + '

set rowcount ' + @pagesize + '

select ' + @fields + ' from   ' + @tablenames + ' ' + @filter   + ' and ' + @sortcolumn + '' + @operator + '@sortcolumnbegin ' + @group + ' order by ' + @order + '    

')    

if @recordcount is null

begin

declare @sql nvarchar(4000)

set @sql=n'select @recordcount=count(*)'

+n' from '+@tablenames

+n' '+@filter

exec sp_executesql @sql,n'@recordcount int output',@recordcount output

endend

sql呼叫:

declare @count int

exec usp_paginglarge '表1,表2,表3','表1 主鍵','要新增的字段',10,1,'條件,多條件時用and連線','','排序字段 desc',@recordcount=@count output

select @count

c#呼叫:

listme = he.usp_paginglarge("表1,表2,表3", "表1 主鍵", "要新增的字段", 10, 1, "條件,多條件時用and連線", "", "排序字段 desc", count).tolist();

console.writeline(count.value);

分頁儲存過程支援多表聯合

create procedure sp paging tblname varchar 255 表名 strgetfields varchar 1000 需要返回的列 fldname varchar 255 排序的欄位名 pagesize int 10,頁尺寸 pageindex int 1,頁碼 r...

多表查詢分頁儲存過程

set ansi nulls on set quoted identifier on go 支援多表查詢分頁儲存過程 事理改進 2012.3 多表聯查1 declare count int exec proc datapagination sl article a,sl user u u.realn...

千萬數量級分頁儲存過程 可支援多表查詢,任意排序

create procedure usp paginglarge tablenames varchar 200 表名,可以是多個表,但不能用別名 primarykey varchar 100 主鍵,可以為空,但 order為空時該值不能為空 fields varchar 200 要取出的字段,可以是...