• SQL 相关分页方法


    【1】

    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER OFF
    GO

    ALTER PROCEDURE [dbo].[procCom_Get_Pagination]
    (
    @Field_SQL varchar(2000), -- 返回的字段SQL
    @From_SQL varchar(1000), -- From中的SQL
    @Where_SQL varchar(4000), -- WhereSQL
    @Order_SQL varchar(255), -- 排序字段
    @Current_Page int, -- 当前显示的页码
    @Page_Size int, -- 每页条数 如果小于等于0 表示不分页
    @Total_Record int output, -- 总记录数
    @Total_Page int output -- 总页数
    )AS
    BEGIN
    DECLARE
    @v_SelectSQL varchar(4000)

    SET NOCOUNT ON;

    BEGIN TRY
    IF LEN(ltrim(rtrim(@Where_SQL)))=0
    SET @Where_SQL='1=1';

    IF @Current_Page<=0
    SET @Current_Page=1;

    SET @v_SelectSQL='Select count(1) from '+@From_SQL+' Where '+@Where_SQL;

    EXEC procCom_Calc_Total_Page @Count_SQL =@v_SelectSQL,
    @Page_Size = @Page_Size,
    @Total_Record = @Total_Record OUTPUT,
    @Total_Page = @Total_Page OUTPUT

    SELECT @v_SelectSQL=dbo.funcCom_Get_Pagination_SQL(@Field_SQL,@From_SQL,@Where_SQL,@Order_SQL,@Current_Page,@Page_Size,@Total_Page);

    --测试的时候打印,正式环境注销打印
    --PRINT @v_SelectSQL;
    SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;

    EXEC(@v_SelectSQL);

    SET TRANSACTION ISOLATION LEVEL READ COMMITTED;

    END TRY
    BEGIN CATCH
    SET TRANSACTION ISOLATION LEVEL READ COMMITTED;

    --记录数据库的错误信息
    DECLARE @Note varchar(2000);
    SET @Note='procCom_Get_Pagination SQL='+@v_SelectSQL;

    EXEC procCom_Log_DB_Msg_Error @Note =@Note;

    RETURN ERROR_NUMBER();
    END CATCH
    END

    【2】

    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER OFF
    GO

    ALTER PROCEDURE [dbo].[procCom_Calc_Total_Page]
    (
    @Count_SQL varchar(4000), -- Select count(1) from ** 的SQL
    @Page_Size int, -- 每页条数
    @Total_Record int output, -- 总记录数
    @Total_Page int output -- 总页数
    )AS
    BEGIN
    SELECT @Total_Record=0,@Total_Page=0;

    BEGIN TRY
    DECLARE @v_Num_Table table
    (
    Num int
    )
    SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;

    INSERT @v_Num_Table EXEC(@Count_SQL);

    SET TRANSACTION ISOLATION LEVEL READ COMMITTED;

    SELECT @Total_Record=Num FROM @v_Num_Table;

    --分页显示 默认每一页显示20条
    IF (@Page_Size<=0 OR @Total_Record<=@Page_Size)
    SET @Total_Page=1
    ELSE
    BEGIN
    IF(@Total_Record%@Page_Size!=0)
    SET @Total_Page=@Total_Record/@Page_Size+1;
    ELSE
    SET @Total_Page=@Total_Record/@Page_Size;
    END

    RETURN 0;
    END TRY
    BEGIN CATCH
    SET TRANSACTION ISOLATION LEVEL READ COMMITTED;

    --记录数据库的错误信息
    DECLARE @Note varchar(2000);
    SET @Note='procCom_Calc_Total_Page SQL='+@Count_SQL;

    EXEC procCom_Log_DB_Msg_Error @Note =@Note;

    RETURN ERROR_NUMBER();
    END CATCH
    END

    【3】

    SET ANSI_NULLS OFF
    GO
    SET QUOTED_IDENTIFIER OFF
    GO

    ALTER FUNCTION [dbo].[funcCom_Get_Pagination_SQL]
    (
    @Field_SQL varchar(2000), -- 返回的字段SQL
    @From_SQL varchar(1000), -- From中的SQL
    @Where_SQL varchar(2000), -- WhereSQL
    @Order_SQL varchar(255), -- 排序字段
    @Current_Page int, -- 当前显示的页码
    @Page_Size int, -- 每页条数 如果小于等于0 表示不分页
    @Total_Page int -- 总页数
    )
    RETURNS varchar(4000)
    AS
    BEGIN
    DECLARE
    @v_SelectSQL varchar(4000)

    IF @Current_Page<=0
    SELECT @Current_Page=1;

    --不分页 或 页数=1
    IF(@Page_Size<=0 or @Total_Page<=1)
    BEGIN
    SELECT @v_SelectSQL='SELECT '+@Field_SQL+' FROM '+@From_SQL+' Where '+@Where_SQL +' order by '+@Order_SQL;
    END
    ELSE
    BEGIN
    SELECT @v_SelectSQL='SELECT '+@Field_SQL+' FROM (SELECT '+@Field_SQL+' ,Row_Number() OVER(order by '+@Order_SQL+') as Inner_Row_ID '
    +' FROM '+@From_SQL+' Where '+@Where_SQL
    +' ) list WHERE Inner_Row_ID between '+CONVERT(varchar,@Page_Size)+'*'+CONVERT(varchar,@Current_Page)
    +'-'+CONVERT(varchar,@Page_Size)+'+1 AND '+CONVERT(varchar,@Page_Size)+'*'+CONVERT(varchar,@Current_Page);
    END
    RETURN(@v_SelectSQL);
    END

    【5】

    EXEC procCom_Get_Pagination
    @Field_SQL = ' * ',
    @From_SQL = ' #Temp_Stat',
    @Where_SQL = '',
    @Order_SQL = @v_order_sql,
    @Current_Page = @Current_Page,
    @Page_Size = @Page_Size,
    @Total_Record = @Total_Record OUTPUT,
    @Total_Page = @Total_Page OUTPUT

  • 相关阅读:
    软件工程——第十次作业
    第33次Scrum会议(11/21)【欢迎来怼】
    软件工程——第九次作业
    第26次Scrum会议(11/14)【欢迎来怼】
    软件工程——第八次作业
    psp总结报告
    第十二周psp
    第十一周psp
    探路者团队-贪吃蛇(测评人:刘耀泽)
    “Hello World!”团队第六周第七次会议
  • 原文地址:https://www.cnblogs.com/lvjy-net/p/8630222.html
Copyright © 2020-2023  润新知