MsSql 存儲(chǔ)過(guò)程分頁(yè)代碼 [收集多篇]
發(fā)布日期:2022-02-01 18:14 | 文章來(lái)源:源碼之家
復(fù)制代碼 代碼如下:
--使用說(shuō)明 本代碼適用于MsSql2000,對(duì)于其它數(shù)據(jù)庫(kù)也可用.但沒(méi)必要
--創(chuàng)建存儲(chǔ)過(guò)程
CREATE PROCEDURE pagination
@tblName varchar(255), -- 表名
@strGetFields varchar(1000) = '*', -- 需要返回的列
@fldName varchar(255)='', -- 排序的字段名(可包含如TABLE.FLDNAME形式)
@PageSize int = 10, -- 頁(yè)尺寸
@PageIndex int = 1, -- 頁(yè)碼
@doCount bit = 0, -- 返回記錄總數(shù), 非 0 值則返回
@OrderType bit = 0, -- 設(shè)置排序類(lèi)型, 非 0 值則降序
@strWhere varchar(1500) = '' -- 查詢(xún)條件 (注意: 不要加 where)
AS
declare @strSQL varchar(5000) -- 主語(yǔ)句
declare @strTmp varchar(110) -- 臨時(shí)變量
declare @strOrder varchar(400) -- 排序類(lèi)型
declare @fldName_t varchar(255) -- 在分頁(yè)時(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傳遞過(guò)來(lái)的不是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
--如果是第一頁(yè)就執(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
--測(cè)試
create table news --建表
(
n_id int iDENTITY(1,1) primary key,
n_title char(200),
n_content text
)
--寫(xiě)循環(huán)插入1000000條的數(shù)據(jù)
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,''
復(fù)制代碼 代碼如下:
自己改寫(xiě)的一個(gè)分頁(yè)存儲(chǔ)過(guò)程
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
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
相關(guān)文章
關(guān)注官方微信