create procedure pagination
@tblname varchar(255), -- 表名
@strgetfields varchar(1000) = '*', -- 需要返回的列
@fldname varchar(255)='', -- 排序的欄位名
@keyname varchar(255)='',--關鍵字段
@pagesize int = 10, -- 頁尺寸
@pageindex int = 1, -- 頁碼
@strwhere varchar(1500) = '' -- 查詢條件 (注意: 不要加 where)
as declare @strsql varchar(5000) -- 主語句
declare @strorder varchar(400) -- 排序型別
begin
begin
set @strorder = " order by " + @fldname
--如果@ordertype不是0,就執行降序,這句很重要!
end
if @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
--如果是第一頁就執行以上**,這樣會加快執行速度
end
else
begin
--以下**賦予了@strsql以真正執行的sql**
set @strsql ="select * from (select top " + str(@pagesize) +" "+@strgetfields+ " from " + @tblname + " where " + @keyname + " not in ( select top " + str(@pagesize*(@pageindex-1)) +" " + @keyname + " from " + @tblname + " " + @strorder + ") " + @strorder + ") tempa "
if @strwhere != ''
set @strsql ="select * from (select top " + str(@pagesize) +" "+@strgetfields+ " from " + @tblname + " where " + @keyname + " not in ( select top " + str(@pagesize*(@pageindex-1)) +" " + @keyname + " from " + @tblname + " where "+ @strwhere + " " + @strorder + ") and " + @strwhere + " " + @strorder + ") tempa"
end
end
exec (@strsql)
go
通用儲存過程 分頁儲存過程
名稱 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...