復制代碼 代碼如下:
--使用說明 本代碼適用於MsSql2000,對於其它數據庫也可用.但沒必要
--創建存儲過程
CREATE PROCEDURE pagination
@tblName varchar(255), -- 表名
@strGetFields varchar(1000) = '*', -- 需要返回的列
@fldName varchar(255)='', -- 排序的字段名(可包含如TABLE.FLDNAME形式)
@PageSize int = 10, -- 頁尺寸
@PageIndex int = 1, -- 頁碼
@doCount bit = 0, -- 返回記錄總數, 非 0 值則返回
@OrderType bit = 0, -- 設置排序類型, 非 0 值則降序
@strWhere varchar(1500) = '' -- 查詢條件 (注意: 不要加 where)
AS
declare @strSQL varchar(5000) -- 主語句
declare @strTmp varchar(110) -- 臨時變量
declare @strOrder varchar(400) -- 排序類型
declare @fldName_t varchar(255) -- 在分頁時用的排序字段名,不包含多表並列時的表名
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,就執行總數統計。以下的所有代碼都是@doCount為0的情況
else
begin
if @OrderType != 0
begin
set @strTmp = '<(select min'
set @strOrder = ' order by ' + @fldName +' desc'
--如果@OrderType不是0,就執行降序,這句很重要!
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
--如果是第一頁就執行以上代碼,這樣會加快執行速度
end
else
begin
--以下代碼賦予了@strSQL以真正執行的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
)
--寫循環插入1000000條的數據
create proc tt
as
declare @i int
set @i=0
while(@i<1000000)
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,''
第二篇
復制代碼 代碼如下:
自己改寫的一個分頁存儲過程
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