CREATE ALTER PROCEDURE [dbo].[sp_insert_time] @type INT --功能类型:"0"增量,"1"全量
AS
SET NOCOUNT ON;
BEGIN
DECLARE @data DATETIME; --当前日期
DECLARE @begin_time DATETIME; --起始时间
DECLARE @end_time DATETIME; --截止日期
--设置周一为每周的第一天
SET DATEFIRST 1;
--获取当前日期(2021-11-11 00:00:00.000)
SET @data = CAST(CONVERT(VARCHAR(8), GETDATE(), 112) AS DATETIME);
--设置截止时间(当天日期向后追加一年)
SET @end_time = DATEADD(yy, 1, @data);
--判断功能类型-增量
IF @type = 0
BEGIN
--日期增量(如果表中最大日期小于明年今日)
IF(SELECT MAX(date) FROM dbo.d_time) < @end_time
BEGIN
--设置起始时间(表中存在最大日期+1天)
SET @begin_time = DATEADD(DAY,1,(SELECT MAX(date) FROM dbo.d_time));
--插入开始
WHILE @begin_time <= @end_time
BEGIN
SET @data = @begin_time;
INSERT INTO [dbo].[d_time]
(
[id],
[date],
[name],
[year],
[year_quarter],
[year_month],
[year_week],
[quarter],
[month],
[week],
[day],
[year_begin_date],
[year_end_date],
[quarter_begin_date],
[quarter_end_date],
[month_begin_date],
[month_end_date],
[week_begin_date],
[week_end_date]
)
VALUES
(cast(CONVERT(varchar(12),@data,112) AS INT )
,CONVERT(varchar(10),@data,120)
,CAST(YEAR(@data) AS VARCHAR)+'年'+RIGHT((100+ MONTH(@data)),2)+'月'+RIGHT(100+DAY(@data),2)+'日'
,CAST(YEAR(@data) AS int)
,YEAR(@data)*10 + CEILING(MONTH(@data)/3.0)
,YEAR(@data)*100 + MONTH(@data)
,YEAR(@data)*100 + DATEPART(WK,@data)
,CEILING(MONTH(@data)/3.0)
,MONTH(@data)
,DATEPART(WK,@data)
,DAY(@data)
,dateadd(year, datediff(year, 0, @data), 0)
,dateadd(year, datediff(year, 0, dateadd(year, 1, @data)), -1)
,DATEADD(qq, DATEDIFF(qq,0,getdate()), 0)
,CONVERT(datetime,CONVERT(char(8),DATEADD(Month,DATEPART(Quarter,@data)*3-Month(@data),@data),120)+CASE WHEN DATEPART(Quarter,@data) in(1,4)THEN'31'ELSE'30'END)
,dateadd(month, datediff(month, 0, @data), 0)
,dateadd(month, datediff(month, 0, dateadd(month, 1, @data)), -1)
,dateadd(day,1-(datepart(weekday,@data)),@data)
,DATEADD(day,1-(datepart(weekday,@data))+6,@data));
SET @begin_time = DATEADD(DAY, 1, @begin_time);
END;
END;
END;
----判断功能类型-全量
IF @type = 1
BEGIN
--全量前清空日期维度表
TRUNCATE TABLE [dbo].[d_time]
--先插入一条null数据
BEGIN
SET @data ='1900-01-01 00:00:00.000';
INSERT INTO [dbo].[d_time]
(
[id],
[date],
[name],
[year],
[year_quarter],
[year_month],
[year_week],
[quarter],
[month],
[week],
[day],
[year_begin_date],
[year_end_date],
[quarter_begin_date],
[quarter_end_date],
[month_begin_date],
[month_end_date],
[week_begin_date],
[week_end_date]
)
VALUES
(cast(CONVERT(varchar(12),@data,112) AS INT )
,CONVERT(varchar(10),@data,120)
,CAST(YEAR(@data) AS VARCHAR)+'年'+RIGHT((100+ MONTH(@data)),2)+'月'+RIGHT(100+DAY(@data),2)+'日'
,CAST(YEAR(@data) AS int)
,YEAR(@data)*10 + CEILING(MONTH(@data)/3.0)
,YEAR(@data)*100 + MONTH(@data)
,YEAR(@data)*100 + DATEPART(WK,@data)
,CEILING(MONTH(@data)/3.0)
,MONTH(@data)
,DATEPART(WK,@data)
,DAY(@data)
,dateadd(year, datediff(year, 0, @data), 0)
,dateadd(year, datediff(year, 0, dateadd(year, 1, @data)), -1)
,DATEADD(qq, DATEDIFF(qq,0,getdate()), 0)
,CONVERT(datetime,CONVERT(char(8),DATEADD(Month,DATEPART(Quarter,@data)*3-Month(@data),@data),120)+CASE WHEN DATEPART(Quarter,@data) in(1,4)THEN'31'ELSE'30'END)
,dateadd(month, datediff(month, 0, @data), 0)
,dateadd(month, datediff(month, 0, dateadd(month, 1, @data)), -1)
,dateadd(day,1-(datepart(weekday,@data)),@data)
,DATEADD(day,1-(datepart(weekday,@data))+6,@data));
END
--正式数据插入开始
SET @begin_time = '2018-01-01 00:00:00.000';
WHILE @begin_time < DATEADD(DAY, 1, @end_time)
BEGIN
SET @data = @begin_time;
--插入开始
INSERT INTO [dbo].[d_time]
(
[id],
[date],
[name],
[year],
[year_quarter],
[year_month],
[year_week],
[quarter],
[month],
[week],
[day],
[year_begin_date],
[year_end_date],
[quarter_begin_date],
[quarter_end_date],
[month_begin_date],
[month_end_date],
[week_begin_date],
[week_end_date]
)
VALUES
(cast(CONVERT(varchar(12),@data,112) AS INT )
,CONVERT(varchar(10),@data,120)
,CAST(YEAR(@data) AS VARCHAR)+'年'+RIGHT((100+ MONTH(@data)),2)+'月'+RIGHT(100+DAY(@data),2)+'日'
,CAST(YEAR(@data) AS int)
,YEAR(@data)*10 + CEILING(MONTH(@data)/3.0)
,YEAR(@data)*100 + MONTH(@data)
,YEAR(@data)*100 + DATEPART(WK,@data)
,CEILING(MONTH(@data)/3.0)
,MONTH(@data)
,DATEPART(WK,@data)
,DAY(@data)
,dateadd(year, datediff(year, 0, @data), 0)
,dateadd(year, datediff(year, 0, dateadd(year, 1, @data)), -1)
,DATEADD(qq, DATEDIFF(qq,0,getdate()), 0)
,CONVERT(datetime,CONVERT(char(8),DATEADD(Month,DATEPART(Quarter,@data)*3-Month(@data),@data),120)+CASE WHEN DATEPART(Quarter,@data) in(1,4)THEN'31'ELSE'30'END)
,dateadd(month, datediff(month, 0, @data), 0)
,dateadd(month, datediff(month, 0, dateadd(month, 1, @data)), -1)
,dateadd(day,1-(datepart(weekday,@data)),@data)
,DATEADD(day,1-(datepart(weekday,@data))+6,@data));
SET @begin_time = DATEADD(DAY, 1, @begin_time);
END;
END;
END;