--支援任意排序的分頁儲存過程
create procedure pagination
@tblname varchar(255), -- 表名
@strgetfields varchar(1000) = '*', -- 需要返回的列
@fldname varchar(255)='', -- 排序的欄位名
@pagesize int , -- 頁尺寸
@pageindex int, -- 頁碼
@docount bit , -- 返回記錄總數, 非 0 值則返回
@ordertype bit , -- 設定排序型別, 非 0 值則降序
@strwhere varchar(1500) = '' -- 查詢條件 (注意: 不要加 where)
asdeclare @strsql varchar(5000) -- 主語句
declare @strtmp varchar(110) -- 臨時變數
declare @strorder varchar(400) -- 排序型別
if @docount != 0
begin
if @strwhere !=''
set @strsql = "select count(*) as total from [" + @tblname + "] where "+@strwhere
else
set @strsql = "select count(*) as total from [" + @tblname + "]"
end--以上**的意思是如果@docount傳遞過來的不是0,就執行總數統計。以下的所有**都是@docount為0的情況
else
begin
if @ordertype != 0
begin
set @strtmp = "<(select min"
set @strorder = " order by [" + @fldname +"] desc"
--如果@ordertype不是0,就執行降序,這句很重要!
endelse
begin
set @strtmp = ">(select max"
set @strorder = " order by [" + @fldname +"] asc"
endif @pageindex = 1
begin
if @strwhere != ''
set @strsql = "select top " + str(@pagesize) +" "+@strgetfields+ " from [" + @tblname + "] where " + @strwhere + " " + @strorder
else
set @strsql = "select top " + str(@pagesize) +" "+@strgetfields+ " from ["+ @tblname + "] "+ @strorder
--如果是第一頁就執行以上**,這樣會加快執行速度
endelse
begin
--以下**賦予了@strsql以真正執行的sql**
set @strsql = "select top " + str(@pagesize) +" "+@strgetfields+ " from ["
+ @tblname + "] where [" + @fldname + "]" + @strtmp + "(["+ @fldname + "]) from (select top " + str((@pageindex-1)*@pagesize) + " ["+ @fldname + "] from [" + @tblname + "]" + @strorder + ") as tbltmp)"+ @strorder
if @strwhere != ''
set @strsql = "select top " + str(@pagesize) +" "+@strgetfields+ " from ["
+ @tblname + "] where [" + @fldname + "]" + @strtmp + "(["
+ @fldname + "]) from (select top " + str((@pageindex-1)*@pagesize) + " ["
+ @fldname + "] from [" + @tblname + "] where " + @strwhere + " "
+ @strorder + ") as tbltmp) and " + @strwhere + " " + @strorder
endend
exec (@strsql)
go呼叫的程式(為了通用性,我寫了乙個方法,大家可以提意見,也可以對其進行修改,多多交換意見,共同進步)
資料海量分頁儲存過程(2)
use forepress go 物件 storedprocedure dbo usp downloaddata 指令碼日期 02 28 2008 19 06 40 set ansi nulls on goset quoted identifier on go 分頁讀取井資料儲存過程 alter p...
資料海量分頁儲存過程(3)
declare strsql varchar 6000 主語句 declare strtmp varchar 1000 臨時變數 declare strorder varchar 500 排序型別 if ordertype 0 begin set strtmp select min set stro...
mysql 通用儲存過程分頁 海量資料分頁
老話題了,不多說了看 吧 測試表create table if not exists test id int 11 not null auto increment,sort int 11 title varchar 50 primary key id engine innodb default ch...