MSSQL中怎么實(shí)現(xiàn)分頁存儲(chǔ)過程,相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。
成都創(chuàng)新互聯(lián)是創(chuàng)新、創(chuàng)意、研發(fā)型一體的綜合型網(wǎng)站建設(shè)公司,自成立以來公司不斷探索創(chuàng)新,始終堅(jiān)持為客戶提供滿意周到的服務(wù),在本地打下了良好的口碑,在過去的十多年時(shí)間我們累計(jì)服務(wù)了上千家以及全國(guó)政企客戶,如食品包裝袋等企業(yè)單位,完善的項(xiàng)目管理流程,嚴(yán)格把控項(xiàng)目進(jìn)度與質(zhì)量監(jiān)控加上過硬的技術(shù)實(shí)力獲得客戶的一致稱贊。
具體如下:
USE [DB_Common]GO/****** 對(duì)象: StoredProcedure [dbo].[Com_Pagination] 腳本日期: 03/09/2012 23:46:20 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO/************************************************************ * *Sql分頁存儲(chǔ)過程(支持多表分頁存儲(chǔ)) * *調(diào)用實(shí)例: EXEC Com_Pagination 100, --總記錄數(shù) 0, --總頁數(shù) -- 'Person',--查詢的表名 ' Person p LEFT JOIN TE a ON a.PID=p.Id ', --查詢的表名(這里為多表) 'a.*', --查詢數(shù)據(jù)列 'p.ID', --排列字段 'p.ID', --分組字段 2, --每頁記錄數(shù) 1, --當(dāng)前頁數(shù) 0, --是否使用分組,否是 ' a.pid=2'--查詢條件 ************************************************************/CREATE PROCEDURE [dbo].[Com_Pagination]@TotalCount INT OUTPUT, --總記錄數(shù)@TotalPage INT OUTPUT, --總頁數(shù)@Table NVARCHAR(1000), --查詢的表名(可多表,例如:Person p LEFT JOIN TE a ON a.PID=p.Id )@Column NVARCHAR(1000), --查詢的字段,可多列或者為*@OrderColumn NVARCHAR(100), --排序字段@GroupColumn NVARCHAR(150), --分組字段@PageSize INT, --每頁記錄數(shù)@CurrentPage INT, --當(dāng)前頁數(shù)@Group TINYINT, --是否使用分組,否是@Condition NVARCHAR(4000) --查詢條件(注意:若這時(shí)候?yàn)槎啾聿樵儯@里也可以跟條件,例如:a.pid=2)ASDECLARE @PageCount INT, --總頁數(shù) @strSql NVARCHAR(4000), --主查詢語句 @strTemp NVARCHAR(2000), --臨時(shí)變量 @strCount NVARCHAR(1000), --統(tǒng)計(jì)語句 @strOrderType NVARCHAR(1000) --排序語句BEGINSET @PageCount = @PageSize * (@CurrentPage -1)SET @strOrderType = ' ORDER BY ' + @OrderColumn + ' 'IF @Condition != ''BEGIN IF @CurrentPage = 1 BEGIN IF @GROUP = 1 BEGIN SET @strCount = 'SELECT @TotalCount=COUNT(*) FROM ' + @Table + ' WHERE ' + @Condition + ' GROUP BY ' + @GroupColumn SET @strCount = @strCount + ' SET @TotalCount=@@ROWCOUNT' SET @strSql = 'SELECT TOP ' + STR(@PageSize) + ' ' + @Column + ' FROM ' + @Table + ' WHERE ' + @Condition + ' GROUP BY ' + @GroupColumn + ' ' + @strOrderType END ELSE BEGIN SET @strCount = 'SELECT @TotalCount=COUNT(*) FROM ' + @Table + ' WHERE ' + @Condition SET @strSql = 'SELECT TOP ' + STR(@PageSize) + ' ' + @Column + ' FROM ' + @Table + ' WHERE ' + @Condition + ' ' + @strOrderType END END ELSE BEGIN IF @GROUP = 1 BEGIN SET @strCount = 'SELECT @TotalCount=COUNT(*) FROM ' + @Table + ' WHERE ' + @Condition + ' GROUP BY ' + @GroupColumn SET @strCount = @strCount + ' SET @TotalCount=@@ROWCOUNT' SET @strSql = 'SELECT * FROM (SELECT TOP (2000) ' + @Column + ',ROW_NUMBER() OVER(' + @strOrderType + ') AS NUM FROM ' + @Table + ' WHERE ' + @Condition + ' GROUP BY ' + @GroupColumn + ') AS T WHERE NUM BETWEEN ' + STR(@PageCount + 1) + ' AND ' + STR(@PageCount + @PageSize) END ELSE BEGIN SET @strCount = 'SELECT @TotalCount=COUNT(*) FROM ' + @Table + ' WHERE ' + @Condition SET @strSql = 'SELECT * FROM (SELECT TOP (2000) ' + @Column + ',ROW_NUMBER() OVER(' + @strOrderType + ') AS NUM FROM ' + @Table + ' WHERE ' + @Condition + ') AS T WHERE NUM BETWEEN ' + STR(@PageCount + 1) + ' AND ' + STR(@PageCount + @PageSize) END ENDENDELSE --沒有查詢條件BEGIN IF @CurrentPage = 1 BEGIN IF @GROUP = 1 BEGIN SET @strCount = 'SELECT @TotalCount=COUNT(*) FROM ' + @Table + ' GROUP BY ' + @GroupColumn SET @strCount = @strCount + 'SET @TotalCount=@@ROWCOUNT' SET @strSql = 'SELECT TOP ' + STR(@PageSize) + ' ' + @Column + ' FROM ' + @Table + ' GROUP BY ' + @GroupColumn + ' ' + @strOrderType END ELSE BEGIN SET @strCount = 'SELECT @TotalCount=COUNT(*) FROM ' + @Table SET @strSql = 'SELECT TOP ' + STR(@PageSize) + ' ' + @Column + ' FROM ' + @Table + ' ' + @strOrderType END END ELSE BEGIN IF @GROUP = 1 BEGIN SET @strCount = 'SELECT @TotalCount=COUNT(*) FROM ' + @Table + ' GROUP BY ' + @GroupColumn SET @strCount = @strCount + 'SET @TotalCount=@@ROWCOUNT' SET @strSql = 'SELECT * FROM (SELECT TOP (2000) ' + @Column + ',ROW_NUMBER() OVER(' + @strOrderType + ') AS NUM FROM ' + @Table + ' GROUP BY ' + @GroupColumn + ') AS T WHERE NUM BETWEEN ' + STR(@PageCount + 1) + ' AND ' + STR(@PageCount + @PageSize) END ELSE BEGIN SET @strCount = 'SELECT @TotalCount=COUNT(*) FROM ' + @Table SET @strSql = 'SELECT * FROM (SELECT TOP (2000) ' + @Column + ',ROW_NUMBER() OVER(' + @strOrderType + ') AS NUM FROM ' + @Table + ') AS T WHERE NUM BETWEEN ' + STR(@PageCount + 1) + ' AND ' + STR(@PageCount + @PageSize) END ENDENDEXEC sp_executesql @strCount, N'@TotalCount INT OUTPUT', @TotalCount OUTPUTIF @TotalCount > 2000BEGIN SET @TotalCount = 2000ENDIF @TotalCount%@PageSize = 0BEGIN SET @TotalPage = @TotalCount / @PageSizeENDELSEBEGIN SET @TotalPage = @TotalCount / @PageSize + 1ENDSET NOCOUNT ONEXEC (@strSql)ENDSET NOCOUNT OFF/**調(diào)用實(shí)例:EXEC Com_Pagination 100, --總記錄數(shù) 0, --總頁數(shù) -- 'Person',--查詢的表名 ' Person p LEFT JOIN TE a ON a.PID=p.Id ', --查詢的表名(這里為多表) 'a.*', --查詢數(shù)據(jù)列 'p.ID', --排列字段 'p.ID', --分組字段 2, --每頁記錄數(shù) 1, --當(dāng)前頁數(shù) 0, --是否使用分組,否是 ' a.pid=2'--查詢條件SELECT a.* FROM Person p LEFT JOIN TE a ON a.PID = p.IdWHERE a.pid = 2**/
看完上述內(nèi)容,你們掌握MSSQL中怎么實(shí)現(xiàn)分頁存儲(chǔ)過程的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
網(wǎng)站名稱:MSSQL中怎么實(shí)現(xiàn)分頁存儲(chǔ)過程
URL分享:http://www.rwnh.cn/article24/jjshce.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、網(wǎng)站設(shè)計(jì)公司、網(wǎng)站排名、網(wǎng)站策劃、Google、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)