復(fù)制代碼 代碼如下:
--使用說明 本代碼適用于MsSql2000,對(duì)于其它數(shù)據(jù)庫也可用.但沒必要
--創(chuàng)建存儲(chǔ)過程
CREATE PROCEDURE pagination
@tblName varchar(255), -- 表名
@strGetFields varchar(1000) = '*', -- 需要返回的列
@fldName varchar(255)='', -- 排序的字段名(可包含如TABLE.FLDNAME形式)
@PageSize int = 10, -- 頁尺寸
@PageIndex int = 1, -- 頁碼
@doCount bit = 0, -- 返回記錄總數(shù), 非 0 值則返回
@OrderType bit = 0, -- 設(shè)置排序類型, 非 0 值則降序
@strWhere varchar(1500) = '' -- 查詢條件 (注意: 不要加 where)
AS
declare @strSQL varchar(5000) -- 主語句
declare @strTmp varchar(110) -- 臨時(shí)變量
declare @strOrder varchar(400) -- 排序類型
declare @fldName_t varchar(255) -- 在分頁時(shí)用的排序字段名,不包含多表并列時(shí)的表名
set @fldName_t = right(@fldName,len(@fldName)-CHARINDEX('.',@fldName))
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,就執(zhí)行總數(shù)統(tǒng)計(jì)。以下的所有代碼都是@doCount為0的情況
else
begin
if @OrderType != 0
begin
set @strTmp = '(select min'
set @strOrder = ' order by ' + @fldName +' desc'
--如果@OrderType不是0,就執(zhí)行降序,這句很重要!
end
else
begin
set @strTmp = '>(select max'
set @strOrder = ' order by ' + @fldName +' asc'
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
--如果是第一頁就執(zhí)行以上代碼,這樣會(huì)加快執(zhí)行速度
end
else
begin
--以下代碼賦予了@strSQL以真正執(zhí)行的SQL代碼
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from '+ @tblName + ' where ' + @fldName + ' ' + @strTmp + ' ('+ @fldName_t + ') 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_t + ') from (select top ' + str((@PageIndex-1)*@PageSize) + ' '+ @fldName + ' from ' + @tblName + ' where ' + @strWhere + ' '+ @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder
end
end
exec (@strSQL)
go
--測試
create table news --建表
(
n_id int iDENTITY(1,1) primary key,
n_title char(200),
n_content text
)
--寫循環(huán)插入1000000條的數(shù)據(jù)
create proc tt
as
declare @i int
set @i=0
while(@i1000000)
begin
insert into news(n_title,n_content) values('sb','dsfsdfsd')
set @i=@i+1
end
exec tt
exec pagination 'news','*','n_id',1000,2,0,0,''
第二篇
復(fù)制代碼 代碼如下:
自己改寫的一個(gè)分頁存儲(chǔ)過程
CREATE PROC Paging
(
@pageSize int,
@pageIndex int,
@pageField nvarchar(32),
@countTotal bit=1,
@fieldQuery nvarchar(512),
@tableQuery nvarchar(512),
@whereQuery nvarchar(2048),
@orderQuery nvarchar(512)
)
AS
DECLARE @bdate Datetime
SET @bdate = getdate()
DECLARE @itemcount int
SET @itemcount=@pageIndex*@pageSize
DECLARE @itemlowwer int
SET @itemlowwer=(@pageIndex-1)*@pageSize
DECLARE @cmd nvarchar(3062)
IF @pageIndex=1
SET @cmd ='SELECT TOP ‘+CAST(@pageSize AS NVARCHAR)+' ‘+@fieldQuery+' FROM ‘+@tableQuery+' WHERE ‘+@whereQuery+' ORDER BY ‘+@orderQuery
ELSE
SET @cmd='SELECT ‘+@fieldQuery+' FROM ‘+@tableQuery+' WHERE ‘+@pageField+' IN (SELECT TOP ‘+CAST(@itemcount as nvarchar)+' ‘+@pageField+' FROM ‘+@tableQuery+' WHERE ‘+@whereQuery+' ORDER BY ‘+ @orderQuery+')
AND ‘+@pageField+' NOT IN (SELECT TOP ‘ +CAST(@itemlowwer as nvarchar)+' ‘+@pageField+' FROM ‘+@tableQuery+' WHERE ‘+@whereQuery+' ORDER BY ‘+ @orderQuery+')'
–print @cmd
EXEC(@cmd)
SELECT DATEDIFF( ms , @bdate , getdate() )
IF @countTotal =1
BEGIN
SET @cmd = ‘SELECT COUNT( 0) FROM ‘+@tableQuery+' WHERE ‘+@whereQuery
EXEC(@cmd)
END
GO
您可能感興趣的文章:- mssql 高效的分頁存儲(chǔ)過程分享
- MSSQL MySQL 數(shù)據(jù)庫分頁(存儲(chǔ)過程)
- 淺談基于SQL Server分頁存儲(chǔ)過程五種方法及性能比較
- 五種SQL Server分頁存儲(chǔ)過程的方法及性能比較
- SQL Server 分頁查詢通用存儲(chǔ)過程(只做分頁查詢用)
- sqlserver2005利用臨時(shí)表和@@RowCount提高分頁查詢存儲(chǔ)過程性能示例分享
- SQL Server的通用分頁存儲(chǔ)過程 未使用游標(biāo),速度更快!
- sqlserver 存儲(chǔ)過程分頁(按多條件排序)
- MSSQL分頁存儲(chǔ)過程完整示例(支持多表分頁存儲(chǔ))