通用分页查询存储过程

Posted zhao-yi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通用分页查询存储过程相关的知识,希望对你有一定的参考价值。

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

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) = ‘‘, --排序字段
   @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
    /* Execute dynamic query */    
    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

 

以上是关于通用分页查询存储过程的主要内容,如果未能解决你的问题,请参考以下文章

求一个通用ORACLE存储过程,实现分页和查询?

sqlserver分页存储过程

存储过程分页

原创10万条数据采用存储过程分页实现(Mvc+Dapper+存储过程)

通用分页存储过程

通用分页存储过程,干货无污染