聯合表 分頁儲存過程

2021-06-19 07:58:56 字數 4582 閱讀 1324

set quoted_identifier on 

goset ansi_nulls on

goalter proc p_public_viewpage

/**//*

no_miss 通用分頁儲存過程 2007.3.1

適用於聯合主鍵/單主鍵/存在能確定唯一行列/存在能確定唯一行的多列 (用英文,隔開)

呼叫:第一頁查詢時返回總記錄和總頁數及第一頁記錄:

execute p_public_viewpage_per 'tablename','col1,col2,col3,col4','pk1,pk2,pk3',

'col5>0 and col7<9','pk1 asc,pk2 asc,pk3 asc',0,10,1,

@totalcount output,@totalpagecount output

其它頁呼叫,比如第89頁(假設第一頁查詢時返回總記錄為2000000):

execute p_public_viewpage_per 'tablename','col1,col2,col3,col4','pk1,pk2,pk3',

'col5>0 and col7<9','pk1 asc,pk2 asc,pk3 asc',2000000,10,89,

@totalcount output,@totalpagecount output

*/@tablename varchar(200), --表名

@fieldlist varchar(2000), --顯示列名

@primarykey varchar(100), --單一主鍵或唯一值鍵或聯合主鍵列表(用英文,隔開)或能確定唯一行的多列列表(用英文,隔開)

@where varchar(1000), --查詢條件 不含'where'字元

@order varchar(1000), --排序 不含'order by'字元,用英文,隔開

@recordercount int, --記錄總數 0:會返回總記錄

@pagesize int, --每頁輸出的記錄數

@pageindex int, --當前頁數

@totalcount int output, --返回記錄總數

@totalpagecount int output --返回總頁數

as set nocount on

set @fieldlist = replace(@fieldlist,' ','')

if @fieldlist = '*'

begin set @fieldlist = 'a.*'end

else

begin

set @fieldlist = 'a.' + replace(@fieldlist,',',',a.')

endwhile charindex(', ',@order)>0

begin

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

endif isnull(@tablename,'') = '' or isnull(@primarykey,'') = ''

or @recordercount < 0 or @pagesize < 0 or @pageindex < 0

begin

return

enddeclare @new_where1 varchar(1000)

declare @new_where2 varchar(1000)

declare @new_where3 varchar(1000)

declare @new_where4 varchar(1000)

declare @new_order1 varchar(1000)

declare @new_order2 varchar(1000)

declare @fields varchar(1000)

declare @sql varchar(8000)

declare @sqlcount nvarchar(4000)

set @fields = @primarykey + ','

set @new_where2 = ''

set @new_where4 = ''

if isnull(@where,'') = ''

begin

set @new_where1 = ' '

set @new_where3 = ' where '

endelse

begin

set @new_where1 = ' where ' + @where + ' '

set @new_where3 = ' where 1=1 '

+ replace(' and ' + @where,' and ',' and a.')+ ' and '

endwhile charindex(',',@fields)>0

begin

set @new_where2 = @new_where2

+ 'a.' + ltrim(left(@fields,charindex(',',@fields)-1))

+ ' = b.' + ltrim(left(@fields,charindex(',',@fields)-1)) + ' and '

set @new_where4 = @new_where4

+ 'b.' + ltrim(left(@fields,charindex(',',@fields)-1)) + ' is null and '

set @fields = substring(@fields,charindex(',',@fields)+1,len(@fields))

endset @new_where2 = left(@new_where2,len(@new_where2)-4)

set @new_where4 = left(@new_where4,len(@new_where4)-4)

if isnull(@order,'') = ''

begin

set @new_order1 = ''

set @new_order2 = ''

endelse

begin

set @new_order1 = ' order by ' + @order

set @new_order2 = ' order by '

+ right(replace(',' + @order,',',', a.' ),

len(replace(',' + @order,',',', a.' ))-1)

endset @sqlcount = 'select @totalcount=count(*),@totalpagecount=ceiling((count(*)+0.0)/'

+ cast(@pagesize as varchar)+') from ' + @tablename

+ ' a ' + @new_where1

if @recordercount = 0

begin

exec sp_executesql @sqlcount,n'@totalcount int output,@totalpagecount int output',

@totalcount output,@totalpagecount output

endelse

begin

select @totalcount = @recordercount

endif @pageindex > ceiling((@totalcount+0.0)/@pagesize)

begin

set @pageindex = ceiling((@totalcount+0.0)/@pagesize)

endif @pageindex = 1

begin

set @sql = 'select top ' + str(@pagesize) + ' ' + @fieldlist + ' from '

+ @tablename + ' a'+ @new_where1 + @new_order1

endelse

begin

set @sql = 'select top ' + str(@pagesize) + ' ' + @fieldlist + ' from '

+ @tablename + ' a left join (select top '

+ str(@pagesize*(@pageindex-1))

+ ' ' + @primarykey + ' from ' + @tablename + @new_where1

+ @new_order1 + ' )b on ' + @new_where2 + @new_where3

+ @new_where4 + @new_order2

endexec(@sql) go

set quoted_identifier off

goset ansi_nulls on

go

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

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

MySql 多表聯合查詢分頁儲存過程

分享乙個好用的mysql多表聯合按條件查詢查詢分頁排序儲存過程 procedure structure for ppage drop procedure if exists ppage delimiter create definer root localhost procedure ppage i...

分頁儲存過程 分頁儲存過程

分頁儲存過程 alter proc dbo p pageshow pagesize int,每頁大小 currentpage int out,當前頁 housename nvarchar 50 房產名稱 totalcount int out,總記錄數 totalpage int out 總頁數 as...