MSSQL中怎么實(shí)現(xiàn)分頁(yè)存儲(chǔ)過(guò)程,相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。
成都創(chuàng)新互聯(lián)是創(chuàng)新、創(chuàng)意、研發(fā)型一體的綜合型網(wǎng)站建設(shè)公司,自成立以來(lái)公司不斷探索創(chuàng)新,始終堅(jiān)持為客戶提供滿意周到的服務(wù),在本地打下了良好的口碑,在過(guò)去的十多年時(shí)間我們累計(jì)服務(wù)了上千家以及全國(guó)政企客戶,如食品包裝袋等企業(yè)單位,完善的項(xiàng)目管理流程,嚴(yán)格把控項(xiàng)目進(jìn)度與質(zhì)量監(jiān)控加上過(guò)硬的技術(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分頁(yè)存儲(chǔ)過(guò)程(支持多表分頁(yè)存儲(chǔ)) * *調(diào)用實(shí)例: EXEC Com_Pagination 100, --總記錄數(shù) 0, --總頁(yè)數(shù) -- 'Person',--查詢的表名 ' Person p LEFT JOIN TE a ON a.PID=p.Id ', --查詢的表名(這里為多表) 'a.*', --查詢數(shù)據(jù)列 'p.ID', --排列字段 'p.ID', --分組字段 2, --每頁(yè)記錄數(shù) 1, --當(dāng)前頁(yè)數(shù) 0, --是否使用分組,否是 ' a.pid=2'--查詢條件 ************************************************************/CREATE PROCEDURE [dbo].[Com_Pagination]@TotalCount INT OUTPUT, --總記錄數(shù)@TotalPage INT OUTPUT, --總頁(yè)數(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, --每頁(yè)記錄數(shù)@CurrentPage INT, --當(dāng)前頁(yè)數(shù)@Group TINYINT, --是否使用分組,否是@Condition NVARCHAR(4000) --查詢條件(注意:若這時(shí)候?yàn)槎啾聿樵?,這里也可以跟條件,例如:a.pid=2)ASDECLARE @PageCount INT, --總頁(yè)數(shù) @strSql NVARCHAR(4000), --主查詢語(yǔ)句 @strTemp NVARCHAR(2000), --臨時(shí)變量 @strCount NVARCHAR(1000), --統(tǒng)計(jì)語(yǔ)句 @strOrderType NVARCHAR(1000) --排序語(yǔ)句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 --沒(méi)有查詢條件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, --總頁(yè)數(shù) -- 'Person',--查詢的表名 ' Person p LEFT JOIN TE a ON a.PID=p.Id ', --查詢的表名(這里為多表) 'a.*', --查詢數(shù)據(jù)列 'p.ID', --排列字段 'p.ID', --分組字段 2, --每頁(yè)記錄數(shù) 1, --當(dāng)前頁(yè)數(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)分頁(yè)存儲(chǔ)過(guò)程的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!