1、通过存储过程生成
sql
复制代码
DELIMITER $$
CREATE PROCEDURE dateDim(startDate DATE, endDate DATE)
BEGIN
DECLARE currentDate DATE;
SET currentDate = startDate;
WHILE currentDate <= endDate DO
INSERT INTO sys_calendar (stats_day) VALUES (currentDate);
SET currentDate = DATE_ADD(currentDate, INTERVAL 1 DAY);
END WHILE;
END $$
DELIMITER ;
CALL dateDim('2022-01-01', '2030-12-31');
2、通过递归的公用表表达式生成
sql
复制代码
WITH RECURSIVE date_series AS (
SELECT '2023-01-01' AS date_value
UNION ALL
SELECT DATE_ADD(date_value, INTERVAL 1 DAY)
FROM date_series
WHERE date_value < '2023-12-31'
)
INSERT INTO date_dim (date_value)
SELECT date_value FROM date_series;