use [forepress]
go/****** 物件: storedprocedure [dbo].[usp_downloaddata] 指令碼日期: 02/28/2008 19:06:40 ******/
set ansi_nulls on
goset quoted_identifier on
go--分頁讀取井資料儲存過程
alter procedure [dbo].[usp_downloaddata]
@tblname varchar(255), -- 表名
@pageindex int = 0 , -- 頁碼
@strwhere varchar(1000) = '', --查詢條件(不要加where)
@strorder varchar(1000) = '' , --需要排序的列
@strgetfields varchar(1000) = '*', -- 需要返回的列
@pagesize int = 200 -- 頁尺寸
asdeclare @strsql varchar(5000) -- 主語句
begin
if @pageindex = 0 --若查詢第一頁,使用以下語句
begin
if @strorder != ''
begin
if @strwhere != ''
set @strsql = 'select top ' + str(@pagesize) +' ' + @strgetfields
+ ' from [' + @tblname + '] ' + ' where ' + @strwhere
+ ' order by ' + @strorder
else
set @strsql = 'select top ' + str(@pagesize) +' ' + @strgetfields
+ 'from [' + @tblname + '] order by ' + @strorder
endelse
begin
if @strwhere != ''
set @strsql = 'select ' +' ' + @strgetfields
+ ' from [' + @tblname + '] ' + 'where ' + @strwhere
else
set @strsql = 'select ' + @strgetfields + ' from [' + @tblname + '] '
endend
else --處理非第一頁的sql語句
begin
if @strorder != ''
begin
if @strwhere != ''
set @strsql = 'select top ' + str(@pagesize) +' ' + @strgetfields
+ ' from [' + @tblname + '] '
+ ' where ' + @strorder + ' > ' + '( select max('+ @strorder
+ ') from ( select top ' + str(@pageindex*@pagesize)+ ' '
+ @strorder + ' from [' + @tblname + '] where ' + @strwhere
+ ' order by ' + @strorder + ') as t ) order by ' + @strorder
else
set @strsql = ' select top ' + str(@pagesize) +' ' + @strgetfields
+ ' from [' + @tblname + '] where ' + @strorder + ' > '
+ '(select max( ' + @strorder + ' ) from ( select top ' + str(@pageindex*@pagesize)
+ @strorder + ' from [ ' + @tblname + '] ' + 'order by ' + @strorder
+ ') as t )' + 'order by ' + @strorder
endelse
begin
if @strwhere != ''
set @strsql = 'select ' +' ' + @strgetfields
+ ' from [' + @tblname + '] ' + ' where ' + @strwhere
else
set @strsql = 'select ' +' ' + @strgetfields
+ ' from [' + @tblname + '] '
endend
endexec (@strsql)
資料海量分頁儲存過程(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...
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 charset gb2312...