将 Sql 服务器存储过程转换为 Java

Convert Sql Server stored procedure to Java

我正在将旧报告系统迁移到现代 Java 代码库,偶然发现 Microsoft Sql 服务器存储过程生成 table 日期(年、期间、周开始、周末)。我需要将此代码迁移到 Java 并使其动态化,而不是将其生成 table 并在数据库中占用 space。

寻求 Sql 服务器专家的帮助,帮助我了解这些日期是如何得出的,尤其是 Period 列中的数字

USE [Reporting]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[GenDataforPeriodsTable]
    @enddate VARCHAR(10)
AS
    DECLARE @startdate VARCHAR(10)
BEGIN
    SET NOCOUNT ON; 

    SELECT @startdate = DATEADD(DAY, 1, MAX(WeekEnding)) FROM Periods;

    WITH CTE_DatesTable
    AS
    (
      SELECT CAST(@startdate as date) AS tempdate
      UNION ALL
      SELECT DATEADD(dd, 1, tempdate)
      FROM CTE_DatesTable
      WHERE DATEADD(dd, 1, tempdate) <= @enddate
    )
    INSERT INTO Periods (YEAR, Period, WeekStarting, WeekEnding)
    SELECT YEAR(tempdate) as Year, MONTH(DATEADD(DAY, -3, tempdate)) as Period, 
            DATEADD(DAY, -6, tempdate) as WeekStarting, tempdate as WeekEnding 
    FROM CTE_DatesTable 
    WHERE DATENAME(weekday, tempdate) = 'SUNDAY'        
    OPTION (MAXRECURSION 0)

END
GO

它会像这样生成 table:

Java 8码:

import static java.time.DayOfWeek.SUNDAY;
import static java.time.temporal.TemporalAdjusters.nextOrSame;

import java.time.LocalDate;
static void genDataforPeriodsTable(LocalDate endDate) {
    String sql = "SELECT MAX(WeekEnding) FROM Periods";
    LocalDate maxWeekEnding = /* Result of running query */;
    genDataforPeriodsTable(maxWeekEnding.plusDays(1), endDate);
}

static void genDataforPeriodsTable(LocalDate startDate, LocalDate endDate) {
    System.out.println("Year Period WeekStarting WeekEnding");
    System.out.println("==== ====== ============ ==========");
    for (LocalDate tempdate = startDate.with(nextOrSame(SUNDAY));
                   tempdate.compareTo(endDate) <= 0;
                   tempdate = tempdate.plusDays(7)) {
        int year = tempdate.getYear();
        int period = tempdate.minusDays(3).getMonthValue();
        LocalDate weekStarting = tempdate.minusDays(6);
        LocalDate weekEnding = tempdate;
        System.out.printf("%4d %-6d %-12s %s%n", year, period, weekStarting, weekEnding);
    }
}

测试

genDataforPeriodsTable(LocalDate.of(2019, 10, 25), LocalDate.of(2020, 5, 5));

输出

此处的输出与您所包含的顺序相反,但数据是相同的,除了 2020 年第一周的开始日期错误,如

Year Period WeekStarting WeekEnding
==== ====== ============ ==========
2019 10     2019-10-21   2019-10-27
2019 10     2019-10-28   2019-11-03
2019 11     2019-11-04   2019-11-10
2019 11     2019-11-11   2019-11-17
2019 11     2019-11-18   2019-11-24
2019 11     2019-11-25   2019-12-01
2019 12     2019-12-02   2019-12-08
2019 12     2019-12-09   2019-12-15
2019 12     2019-12-16   2019-12-22
2019 12     2019-12-23   2019-12-29
2020 1      2019-12-30   2020-01-05
2020 1      2020-01-06   2020-01-12
2020 1      2020-01-13   2020-01-19
2020 1      2020-01-20   2020-01-26
2020 1      2020-01-27   2020-02-02
2020 2      2020-02-03   2020-02-09
2020 2      2020-02-10   2020-02-16
2020 2      2020-02-17   2020-02-23
2020 2      2020-02-24   2020-03-01
2020 3      2020-03-02   2020-03-08
2020 3      2020-03-09   2020-03-15
2020 3      2020-03-16   2020-03-22
2020 3      2020-03-23   2020-03-29
2020 4      2020-03-30   2020-04-05
2020 4      2020-04-06   2020-04-12
2020 4      2020-04-13   2020-04-19
2020 4      2020-04-20   2020-04-26
2020 4      2020-04-27   2020-05-03