zoukankan      html  css  js  c++  java
  • 分页SQL 和Oracle 存储过程


    CREATE PROCEDURE [dbo].[sp_GetPagingList](
    @Table nvarchar(1000),          --表名
    @Field nvarchar(1000) = '*',        --读取字段
    @Where  nvarchar(500) = NULL,       --Where条件
    @GroupBy nvarchar(500) = NULL,      --分组
    @OrderBy nvarchar(500),       --排序字段必须
    @PageNumber int = 1,            --开始页码
    @PageSize int = 10,             --页大小
    @IsCount bit = 0            --是否返回记录总数
    )
    AS
    BEGIN
    ------------------------------------------------------------------------------------------------
    DECLARE @strWhere nvarchar(500)                     --Where 条件
    IF @Where IS NOT NULL AND @Where != ''              --Where 条件
    BEGIN
    SET @strWhere = ' WHERE ' + @Where + ' '
    END
    ELSE
    BEGIN
    SET @strWhere = ''
    END
    ----------------------------------------------------------------------------------------------------
    DECLARE @strGroupBy nvarchar(500)                   --GroupBy 条件
    IF @GroupBy IS NOT NULL AND @GroupBy != ''          --GroupBy 条件
    BEGIN
    SET @strGroupBy = ' GROUP BY ' + @GroupBy + ' '
    END
    ELSE
    BEGIN
    SET @strGroupBy = ''
    END
    ----------------------------------------------------------------------------------------------------
    DECLARE @strOrderBy nvarchar(500)                   --OrderBy 条件

    SET @strOrderBy = ' ORDER BY ' + @OrderBy

    ----------------------------------------------------------------------------------------------------
    DECLARE @strSql nvarchar(max)   --Sql 语句

    ----------------------------------------------------------------------------------------------------
    IF @PageNumber < 1                                  --第一页提高性能
    BEGIN   
    SET @PageNumber = 1
    END

    IF @PageNumber = 1                          
    BEGIN
    SET @strSql = 'SELECT TOP ' + str(@PageSize) +  ' ' + @Field + ' FROM ' + @Table + 
       @strWhere + @strGroupBy + @strOrderBy
    EXEC sp_executesql @strSql
    END 
    ----------------------------------------------------------------------------------------------------
    --根据 SqlServer 2005 帮助得到下面的语句 
    ELSE
    BEGIN 
    DECLARE @STARTID nvarchar(50)
    DECLARE @ENDID nvarchar(50)
    SET @STARTID = convert(nvarchar(50),(@PageNumber - 1) * @PageSize + 1)
    SET @ENDID = convert(nvarchar(50),@PageNumber * @PageSize)
    SET @strSql = 'WITH MYTABLE AS (SELECT ROW_NUMBER() OVER (' + @strOrderBy + ')
       AS RowNumber,' + @Field + ' FROM '+ @Table +  @strWhere + @strGroupBy + ') 
       SELECT * FROM MYTABLE 
       WHERE RowNumber BETWEEN ' + @STARTID + ' AND ' + @ENDID

      --oracle 分页语句语法。
      -- select * from (select A.* ,ROWNUM RN from(select * from tb_test)A where ROWNUM<=@ENDID)where RN >=@STARTID
    EXEC sp_executesql @strSql
    END
    --------------------------------------------------------------------------------------------------
    --计算总行数
    IF @IsCount = 1
    BEGIN
    SET @strSql= 'SELECT  Count (*) AS RecordCount FROM ' + @Table + @strWhere + @strGroupBy
    EXEC sp_executesql @strSql   
    END 
    RETURN
    END
    return

  • 相关阅读:
    【重大更新】Qlik Sense September 2018重磅发布(附下载)
    sharepoint多个NLB的web前段如何进行文件同步?
    通过组策略实现Firefox自动以当前域账号登录MOSS站点---(原创)
    EWS API 2.0读取日历信息-读取内容注意事项
    Dotfuscator混淆加密
    如何:在 SharePoint 中创建外部列表
    java学习笔记—HTTP协议(10)
    java学习笔记—Tomcat(9)
    java学习笔记DOM4J解析(7)
    php中mvc框架总结1(7)
  • 原文地址:https://www.cnblogs.com/xiaogelove/p/2342801.html
Copyright © 2011-2022 走看看