我正在将旧的报告系统迁移到现代 Java 代码库,并偶然发现了 Microsoft Sql Server 存储过程,它生成了带有日期(Year、Period、WeekStarting、WeekEnding)的表。我需要将此代码迁移到 Java 中并使其动态化,而不是生成一个表并占用数据库中的空间。
寻求 Sql Server 专家的帮助,以帮助我了解这些日期是如何得出的,尤其是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
它生成这样的表: