通用分页查询存储过程

时间:2022-02-27 04:43:40

分页数据查询通用存储过程,单表

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:大师兄
-- Create date: 2018-07-16
-- Description:分页数据查询通用存储过程,单表
-- =============================================
GO
CREATE PROCEDURE [dbo].[PROC_PAGINATIONBY_SINGLE_TABLE]

@FEILDS  VARCHAR(1000),--要显示的数据字段,*表示所有字段
@TABLE_NAME VARCHAR(100),--要查询的数据表名称
@PAGE_INDEX INT,--当前页码
@PAGE_SIZE INT,--页面大小
@ORDERTYPE BIT,--当为0时 则为 desc 当为1 时 asc
@ANDWHERE VARCHAR(1000)='',--where语句 不用加where
@ORDERFEILD VARCHAR(100) --排序的字段
as
DECLARE @EXECSQL VARCHAR(2000)
DECLARE @ORDERSTR VARCHAR(100)
DECLARE @ORDERBY VARCHAR(100)
BEGIN
    set NOCOUNT on
    IF @ORDERTYPE =1 
        BEGIN
            SET @ORDERSTR =' > ( SELECT MAX(['+@ORDERFEILD+'])'
            SET @ORDERBY ='ORDER BY '+@ORDERFEILD+' ASC'
        END
    ELSE 
        BEGIN
            SET @ORDERSTR =' < ( SELECT MIN(['+@ORDERFEILD+'])'
            SET @ORDERBY ='ORDER BY '+@ORDERFEILD+' DESC'
        END

    IF @PAGE_INDEX =1--当页码是第一页时直接运行,提高速度
        BEGIN
            IF @ANDWHERE=''
                SET @EXECSQL ='SELECT TOP '+STR(@PAGE_SIZE)+''+@FEILDS+' FROM '+ @TABLE_NAME +' '+ @ORDERBY
            ELSE
                SET @EXECSQL ='SELECT TOP '+STR(@PAGE_SIZE)+''+@FEILDS+' FROM '+ @TABLE_NAME +' WHERE '+@ANDWHERE+''+ @ORDERBY
        END
    ELSE
        BEGIN
            IF @ANDWHERE=''
                BEGIN      --以子查询结果当做新表时 要给表名别名才能用
                    SET @EXECSQL ='SELECT TOP'+STR(@PAGE_SIZE)+''+@FEILDS+' FROM '+ @TABLE_NAME +' WHERE '+@ORDERFEILD+
                                @ORDERSTR+' FROM (SELECT TOP '+STR(@PAGE_SIZE*(@PAGE_INDEX-1))+''+@ORDERFEILD+
                                ' FROM '+ @TABLE_NAME +' '+@ORDERBY+') AS TEMP) '+ @ORDERBY
                END
            ELSE
                BEGIN
                    SET @EXECSQL ='SELECT TOP'+STR(@PAGE_SIZE)+''+@FEILDS+' FROM '+ @TABLE_NAME +' WHERE '+@ORDERFEILD+
                                @ORDERSTR+' FROM (SELECT TOP '+ STR(@PAGE_SIZE*(@PAGE_INDEX-1))+''+@ORDERFEILD+
                                ' FROM '+ @TABLE_NAME +' WHERE '+@ANDWHERE+''+@ORDERBY+') AS TEMP) AND '+@ANDWHERE+''+ @ORDERBY
                END
        END
EXEC (@EXECSQL)--这里要加括号
END

分页数据查询通用存储过程,多表

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:大师兄
-- Create date: 2018-07-16
-- Description:分页数据查询通用存储过程,多表
-- =============================================
GO
Create PROCEDURE [dbo].[PROC_PAGINATIONBY_MULTI_TABLE]
(  @TableName VARCHAR(1000), --表名,多表是请使用 tableA a INNER JOIN tableB b ON a.AID = b.AID
   @PrimaryKey NVARCHAR(100),    --主键,可以带表头 a.AID
   @Fields NVARCHAR(2000) = '*',--读取字段
   @Condition NVARCHAR(3000) = '',--Where条件
   @CurrentPage INT = 1,    --开始页码
   @PageSize INT = 10,      --页大小
   @Sort NVARCHAR(200) = '',--排序字段a.AID desc,b.ID asc
   @RecordCount INT = 0 OUT
)
AS
DECLARE @strWhere VARCHAR(2000)
DECLARE @strsql NVARCHAR(3900)
IF @Condition IS NOT NULL AND len(ltrim(rtrim(@Condition)))>0
  BEGIN
   SET @strWhere = ' WHERE ' + @Condition + ' '
  END
ELSE
  BEGIN
   SET @strWhere = ''
  END
        
IF (charindex(ltrim(rtrim(@PrimaryKey)),@Sort)=0)
BEGIN
    IF(@Sort='')
        SET @Sort = @PrimaryKey + ' DESC '
    ELSE
        SET @Sort = @Sort+ ' , '+@PrimaryKey + ' DESC '
END
SET @strsql = 'SELECT @RecordCount = Count(1) FROM ' + @TableName + @strWhere  
EXECUTE sp_executesql @strsql ,N'@RecordCount INT output',@RecordCount OUTPUT
IF @CurrentPage = 1 --第一页提高性能
BEGIN 
  SET @strsql = 'SELECT TOP ' + str(@PageSize) +' '+@Fields+ '  FROM ' + @TableName + ' ' + @strWhere + ' ORDER BY  '+ @Sort
END 
ELSE
  BEGIN
    /* 执行动态查询 */    
    DECLARE @START_ID NVARCHAR(50)
    DECLARE @END_ID NVARCHAR(50)
    SET @START_ID = CONVERT(NVARCHAR(50),(@CurrentPage - 1) * @PageSize + 1)
    SET @END_ID = CONVERT(NVARCHAR(50),@CurrentPage * @PageSize)
    SET @strsql =  ' SELECT * FROM (SELECT ROW_NUMBER() OVER(ORDER BY '+@Sort+') AS rownum, '+@Fields+ 'FROM '+@TableName + @strWhere +') AS XX WHERE rownum BETWEEN '+@START_ID+' AND ' +@END_ID +' ORDER BY XX.rownum ASC'
  END
EXEC(@strsql)
RETURN