分頁(yè)存儲(chǔ)過(guò)程代碼
發(fā)布日期:2022-02-02 18:45 | 文章來(lái)源:源碼中國(guó)
復(fù)制代碼 代碼如下:
/*
*@curentpage 當(dāng)前頁(yè)
*@pagesize 每頁(yè)記錄數(shù)
*@TableName 表名
*@key 主鍵(自動(dòng)排序)
*@where 查詢條件
1)空為 null
2)有查詢條件不要帶where
*@order '0'表示 desc '1'是asc
*@pageCount 總頁(yè)數(shù)
*/
create procedure Page
@currentpage int,@pagesize int,
@TableName varchar(30),@key varchar(30),
@where varchar(50),@order varchar(1),
@pageCount int ,@str varchar(450) output
as
begin
---------------執(zhí)行的sql語(yǔ)句------------
declare @sql nvarchar(400),@ordreby nvarchar(200)
declare @tempsql1 varchar(200),@tempsql2 varchar(200)
---------------記錄總數(shù)-----------------
declare @count int
---------------臨時(shí)變量------------------------
declare @temp1 int,@temp2 int set @TableName=' '+@TableName+' '
set @key=' '+@key+' ' if @order='0'
set @ordreby=' order by '+@key+'desc'
else
set @ordreby=' order by '+@key if @where='null'
set @sql='select @count = count(*) from '+ @TableName
else
set @sql='select @count = count(*) from '+ @TableName+' where '+@where ------------@count 付值(聲明變量@count 在說(shuō)明是output 內(nèi)型)---------------------------
exec sp_executesql @sql,N'@count int out',@count out
------------求總頁(yè)數(shù)------------------------------
if (@count%@pagesize)=0
set @pagecount=@count/@pagesize
else
set @pagecount=@count/@pagesize+1
-----------判斷顯示當(dāng)前頁(yè)是否異常------------------
if @currentpage>@pagecount
set @currentpage=@pagecount
if @currentpage<1
set @currentpage=1
----------記錄數(shù)小于頁(yè)面顯示記錄數(shù)-----------------
if(@currentpage=1)
begin
if @where='null'
set @where=' '
else
set @where=' where '+@where
set @sql = 'select top'+ str(@pagesize)+' * from '+@TableName+@where+@ordreby
end
else
begin
/**//* ---------------desc----------------------
*@temp1表示前面的記錄
*@temp2表示后面的記錄
*假設(shè)一共77個(gè)記錄,每次取10個(gè)。取67~58(第2頁(yè)),去掉前面的57(1~57)個(gè)和后面的10個(gè)(77~66)
*/
if @order=0
begin
set @temp1 = @count-@currentpage*@pagesize
if @temp1<0
set @temp1=0
set @temp2 = (@currentpage - 1)*@pagesize
if @where='null'
begin
set @tempsql1='select top ' + str(@temp1)+' '+@key+' from ' + @TableName+' order by ' +@key
set @tempsql2='select top ' + str(@temp2)+' '+@key+' from ' + @TableName + @ordreby
end
else
begin
set @tempsql1='select top ' + str(@temp1)+' '+@key+' from ' + @TableName+' where '+@where+' order by ' +@key
set @tempsql2='select top ' + str(@temp2)+' '+@key+' from ' + @TableName+' where '+@where+@ordreby
end
set @sql=' select top ' + str(@pagesize) + ' * from ' + @TableName + ' where '+@key+ ' not in '
set @sql= @sql+' ( '+ @tempsql1 +' ) and '
set @sql= @sql+@key+ ' not in ( '+@tempsql2 +' ) '
if @where='null'
set @sql= @sql+@ordreby
else
set @sql= @sql+' and '+@where+@ordreby
end
/**//* ----------------asc---------------------
* @temp 表示前面顯示的記錄總數(shù)
* 去掉 @temp 在取出 pagesize 個(gè)即可
*/
else
begin
set @temp1=(@currentpage-1)*@pagesize
if @where='null'
set @tempsql1='select top '+ str(@temp1)+' '+@key+' from ' + @TableName + @ordreby
else
set @tempsql1='select top '+ str(@temp1)+' '+@key+' from ' + @TableName ++' where '+@where+@ordreby
set @sql=' select top ' + str(@pagesize) + ' * from ' + @TableName + ' where '+@key+ ' not in '
set @sql=@sql+' ( '+@tempsql1+' ) '
if @where='null'
set @sql= @sql+@ordreby
else
set @sql= @sql+' and '+@where+@ordreby end
/**//* -------------------------------------*/
end
set @str=@sql
--exec sp_executesql @sql end GO
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(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)文章