? ? 思路來源:
??? 從publish 表中取出第 n 條到第 m 條的記錄:?
??? SELECT TOP m-n+1 *?
????? FROM publish?
????? WHERE (id NOT IN?
????????????? (SELECT TOP n-1 id?
???????? FROM publish))
??? 存儲過程:
CREATE PROCEDURE pagination3
@tblName varchar(255) , -- 表名
@strGetFields varchar(1000) = '*', -- 需要返回的列?
@fldName varchar(255)='', -- 排序的字段名
@PageSize int = 10, -- 頁尺寸
@PageIndex int = 1, -- 頁碼
@doCount bit = 0, -- 返回記錄總數(shù), 非 0 值則返回
@OrderType bit = 0, -- 設置排序類型, 非 0 值則降序
@strWhere varchar(1500) = '' -- 查詢條件 (注意: 不要加 where)
AS
declare @strSQL varchar(5000) -- 主語句
declare @strTmp varchar(110) -- 臨時變量
declare @strOrder varchar(400) -- 排序類型
if @doCount != 0
?? begin
????? if @strWhere !=''
???????? set @strSQL = 'select count(*)? Total from? where '+@strWhere
????? else
???????? set @strSQL = 'select count(*)? Total from '
?? end?
--以上代碼的意思是如果@doCount傳遞過來的不是0,就執(zhí)行總數(shù)統(tǒng)計。以下的所有代碼都是@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í)行以上代碼,這樣會加快執(zhí)行速度
??????? end
???? else
???????? begin
?????????? set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from ['
??????????????????????? + @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
??????????????????????? + @fldName + ']) 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 + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
??????????????????????? + @fldName + '] from [' + @tblName + '] where ' + @strWhere + ' '
??????????????????????? + @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder
???????? end
?? end
exec (@strSQL)
--print @strSQL
GO
更多文章、技術交流、商務合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
