程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> SqlServer數據庫 >> 關於SqlServer >> MSSQL分頁存儲過程完整示例(支持多表分頁存儲)

MSSQL分頁存儲過程完整示例(支持多表分頁存儲)

編輯:關於SqlServer

本文實例講述了MSSQL分頁存儲過程。分享給大家供大家參考,具體如下:

USE [DB_Common]
GO
/****** 對象: StoredProcedure [dbo].[Com_Pagination]  腳本日期: 03/09/2012 23:46:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/************************************************************
 *
 *Sql分頁存儲過程(支持多表分頁存儲)
 *
 *調用實例:
 EXEC Com_Pagination 100, --總記錄數
   0, --總頁數
    -- 'Person',--查詢的表名
   '
           Person p
           LEFT JOIN TE a
           ON a.PID=p.Id 
          ', --查詢的表名(這裡為多表)
   'a.*', --查詢數據列
   'p.ID', --排列字段
   'p.ID', --分組字段
   2, --每頁記錄數
   1, --當前頁數
   0, --是否使用分組,否是
   ' a.pid=2'--查詢條件
 ************************************************************/
CREATE PROCEDURE [dbo].[Com_Pagination]
@TotalCount INT OUTPUT, --總記錄數
@TotalPage INT OUTPUT, --總頁數
@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, --每頁記錄數
@CurrentPage INT, --當前頁數
@Group TINYINT, --是否使用分組,否是
@Condition NVARCHAR(4000) --查詢條件(注意:若這時候為多表查詢,這裡也可以跟條件,例如:a.pid=2)
AS
DECLARE @PageCount   INT, --總頁數
    @strSql    NVARCHAR(4000), --主查詢語句
    @strTemp    NVARCHAR(2000), --臨時變量
    @strCount   NVARCHAR(1000), --統計語句
    @strOrderType NVARCHAR(1000) --排序語句
BEGIN
SET @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
  END
END
ELSE
  --沒有查詢條件
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
  END
END
EXEC sp_executesql @strCount,
   N'@TotalCount INT OUTPUT',
   @TotalCount OUTPUT
IF @TotalCount > 2000
BEGIN
  SET @TotalCount = 2000
END
IF @TotalCount%@PageSize = 0
BEGIN
  SET @TotalPage = @TotalCount / @PageSize
END
ELSE
BEGIN
  SET @TotalPage = @TotalCount / @PageSize + 1
END
SET NOCOUNT ON
EXEC (@strSql)
END
SET NOCOUNT OFF
/**調用實例:
EXEC Com_Pagination 100, --總記錄數
   0, --總頁數
    -- 'Person',--查詢的表名
   '
           Person p
           LEFT JOIN TE a
           ON a.PID=p.Id 
          ', --查詢的表名(這裡為多表)
   'a.*', --查詢數據列
   'p.ID', --排列字段
   'p.ID', --分組字段
   2, --每頁記錄數
   1, --當前頁數
   0, --是否使用分組,否是
   ' a.pid=2'--查詢條件
SELECT a.* 
FROM  Person p
    LEFT JOIN TE a
      ON a.PID = p.Id
WHERE a.pid = 2
**/

希望本文所述對大家SQL Server數據庫程序設計有所幫助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved