set ansi_nulls on
set quoted_identifier on
goalter procedure [dbo].[pagination]
@tablename varchar(255), -- 表名
@primarykey varchar(255),--主鍵或者唯一約束字段
@docount int = 2, -- 需不需要進行總數統計.2是統計條數及查詢 1是只統計總條數,不進行查詢 0進行分頁查詢,不進行統計
@strwhere varchar(1500) = '', -- 查詢條件 (注意: 不要加 where)
@orderfield varchar(255)=@primarykey, -- 排序的欄位名,預設按照主鍵排序
@pageindex int = 1, -- 頁碼
@pagesize int = 10, -- 頁尺寸
@ordertype bit = 0 ,-- 設定排序型別, 非 0 值則降序
@strgetfields varchar(1000) = '*'-- 需要返回的列
asdeclare @strsql varchar(5000) -- 主語句
declare @strtmp varchar(110) -- 臨時變數
declare @strorder varchar(400) -- 排序型別
@docount傳遞過來的不是0,就執行總數統計
if @docount = 1
begin
if @strwhere!=''
set @strsql = 'select count(*) as total from ' + @tablename + ' where '+@strwhere
else
set @strsql = 'select count(*) as total from ' + @tablename
execute(@strsql)
end以下的所有**都是@docount為0的情況:
if @docount=0
begin
確定是公升序還是降序
if @ordertype != 0
begin
set @strtmp = '<(select min'
set @strorder = 'order by ' + @orderfield +' desc'
endelse
begin
set @strtmp = '>(select max'
set @strorder = 'order by ' + @orderfield +' asc'
end為了加快執行速度,判斷一下是不是第一頁
if @pageindex = 1
begin
if @strwhere != ''
set @strsql ='select top ' + str(@pagesize) +' '+@strgetfields+ ' from ' + @tablename + ' where ' + @strwhere + ' ' + @strorder
else
set @strsql ='select top ' + str(@pagesize) +' '+@strgetfields+ ' from '+ @tablename + ' '+ @strorder
end不是第一頁
else
begin
if @strwhere=''
set @strsql='select top '+str(@pagesize)+' '+@strgetfields+' from '+ @tablename +' where '+@primarykey+@strtmp+'('+@primarykey+') as '+@primarykey+' from (select top '+str((@pageindex-1)*@pagesize)+' '+@primarykey+' from '+@tablename+' '+@strorder+') as t) '+@strorder
else
set @strsql='select top '+str(@pagesize)+' '+@strgetfields+' from '+ @tablename +' where '+@primarykey+@strtmp+'('+@primarykey+') as '+@primarykey+' from (select top '+str((@pageindex-1)*@pagesize)+' '+@primarykey+' from '+@tablename+' where '+@strwhere+' '+@strorder+') as t) and '+@strwhere+' '+@strorder
end--print @strsql --列印sql語句
execute(@strsql)
endif @docount=2
begin
--記錄數
if @strwhere!=''
set @strsql = 'select count(*) as total from ' + @tablename + ' where '+@strwhere
else
set @strsql = 'select count(*) as total from ' + @tablename
execute(@strsql)
--查詢
if @ordertype != 0
begin
set @strtmp = '<(select min'
set @strorder = 'order by ' + @orderfield +' desc'
endelse
begin
set @strtmp = '>(select max'
set @strorder = 'order by ' + @orderfield +' asc'
end為了加快執行速度,判斷一下是不是第一頁
if @pageindex = 1
begin
if @strwhere != ''
set @strsql ='select top ' + str(@pagesize) +' '+@strgetfields+ ' from ' + @tablename + ' where ' + @strwhere + ' ' + @strorder
else
set @strsql ='select top ' + str(@pagesize) +' '+@strgetfields+ ' from '+ @tablename + ' '+ @strorder
end不是第一頁
else
begin
if @strwhere=''
set @strsql='select top '+str(@pagesize)+' '+@strgetfields+' from '+ @tablename +' where '+@primarykey+@strtmp+'('+@primarykey+') as '+@primarykey+' from (select top '+str((@pageindex-1)*@pagesize)+' '+@primarykey+' from '+@tablename+' '+@strorder+') as t) '+@strorder
else
set @strsql='select top '+str(@pagesize)+' '+@strgetfields+' from '+ @tablename +' where '+@primarykey+@strtmp+'('+@primarykey+') as '+@primarykey+' from (select top '+str((@pageindex-1)*@pagesize)+' '+@primarykey+' from '+@tablename+' where '+@strwhere+' '+@strorder+') as t) and '+@strwhere+' '+@strorder
end--print @strsql --列印sql語句
execute(@strsql)
end
通用儲存過程 分頁儲存過程
名稱 spall returnrows 輸入 輸出 呼叫 exec spall returnrows select from 表名 頁號,返回記錄數,主鍵 排序字段 spall returnrows select from all categories 2,10,id id 說明 百萬級 通用儲存過...
通用分頁儲存過程
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...