通用分頁存過程

2021-04-26 10:40:55 字數 3202 閱讀 1084

create procedure [dbo].[usp_getfrontdatalist_byfullindex]

(@tableviewqueryname varchar( 1024 ),--table或view或者query的名字或字串

@key  varchar( 50 ), --關鍵字

@selectstr  varchar( 500 ),  --選擇列字串

@criteria  varchar( 8000 ),--查詢條件

@sort  varchar( 255 ), --排序字串

@fristtopnum  int,   --顯示的第一頁置頂的數目

@page  bigint output, --顯示的當前頁號

@currentpagerow bigint, --頁大小(顯示多少行)

@totalcount  bigint output  --通過該查詢條件,返回的查詢記錄的總頁數

)asset nocount onif charindex(';',@criteria)>0 or charindex('--',@criteria)>0 or charindex('/*',@criteria)>0 or

charindex('*/',@criteria)>0 or charindex('syscolumns',@criteria)>0 orcharindex('sysfiles',@criteria)>0

or charindex('char(124)',@criteria)>0 or charindex('1=1',@criteria)>0

return

declare @totalstr nvarchar(4000)

declare @str  nvarchar(4000)

declare @toprownum bigint

if @selectstr is null and

rtrim(ltrim(@criteria)) = ''

set @selectstr = '*'

if @fristtopnum is null and @fristtopnum < 0

begin

set @fristtopnum = 0

endelse if @fristtopnum > @currentpagerow

begin

set @fristtopnum = @currentpagerow

endif @currentpagerow > 0

begin

if @criteria is not null and

rtrim(ltrim(@criteria)) <> ''

begin

set @totalstr = 'select @totalcount=count(*) from ' +'('+ @tableviewqueryname  +')'+ ' t '

+ ' where ' + @criteria

endelse

begin

set @totalstr = 'select @totalcount=count(*) from ' +'('+@tableviewqueryname +')'+ ' t '

endprint @totalstr

exec sp_executesql @totalstr, n'@totalcount bigint output',@totalcount output

set @totalcount = @totalcount + isnull(@fristtopnum ,0)

declare @totalpage bigint

set @totalpage = @totalcount/@currentpagerow

if @totalcount%@currentpagerow > 0

begin

set @totalpage = @totalpage + 1

endif @page <= 0

begin

set @page = 1

endif @totalpage > 0 and

@page > @totalpage

begin

set @page = @totalpage

end--組織查詢語句

set @str = 'select  ' +  @selectstr + ' from (' +@tableviewqueryname + ') t where t.serialnumber >' +

cast ((@page-1) as varchar(10)) + '*' +cast( @currentpagerow as varchar(10))+' and t.serialnumber <= '+

cast (@page as varchar(10)) + '*' +cast( @currentpagerow as varchar(10))if @sort is not null andrtrim(ltrim(@sort)) <> ''

begin

if @criteria is not null and rtrim(ltrim(@criteria)) <> ''

begin

set @str = @str +  ' and (' + @criteria + ') order by '+@sort

endelse

begin

set @str = @str +  ' and (' + @criteria + ') order by '+@sort

endend

else

begin

if @criteria is not null and  rtrim(ltrim(@criteria)) <> ''

begin

set @str = @str + ' and (' + @criteria + ') ' 

endend

--對無記錄時當前頁數的處理

if @totalcount=0

begin

set @page = 0

endend

exec sp_executesql @str

通用分頁儲存過程

create procedure sp page strtable varchar 50 表名 strcolumn varchar 50 按該列來進行分頁 intcoltype int,strcolumn列的型別,0 數字型別,1 字元型別,2 日期時間型別 intorder bit,排序,0 順序...

通用分頁儲存過程

set quoted identifier on goset ansi nulls off go declare p1 int set p1 null exec sp pageview tbname n v question list fieldkey n id pagecurrent 3,page...

通用分頁儲存過程

通用分頁儲存過程 create proc commonpagination columns varchar 500 要顯示的列名,用逗號隔開 tablename varchar 100 要查詢的表名 ordercolumnname varchar 100 排序的列名 order varchar 50...