以下是代码:
IF EXISTS(_select_ * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Dim_Date]')AND type in(N'U'))
Drop table [dbo].[Dim_Date]
GO
CREATE TABLE [dbo].[Dim_Date](
[TimeKey] [int] NOT NULL,
[TheDate] [datetime] NULL, --DateTime格式的日期
[TheDateName] [nvarchar](10)NULL, --日期名称
[TheYear] [smallint] NULL,--年份
[TheYearName] [nvarchar](10)NULL,--年份名称
[TheMonth] [smallint] NULL,--月份
[TheMonthName] [nvarchar](10)NULL,--月份名称
[TheDay] [smallint] NULL,--日
[TheDayName] [nvarchar](10)NULL,--日的名称
[TheQuarter] [smallint] NULL,--季度
[TheQuarterName] [nvarchar](10)NULL,--季度名称
[TheWeek] [smallint] NULL,--星期
[TheWeekName] [nvarchar](10)NULL,--星期名称
[Vacation_Mark] [smallint] NULL--节假日标志
)ON [PRIMARY]
DECLARE
@TheDate datetime,
@TheDateName nvarchar(10),
@TheYear smallint,
@TheYearName nvarchar(10),
@TheMonth smallint,
@TheMonthName nvarchar(10),
@TheDay smallint,
@TheDayName nvarchar(10),
@TheQuarter smallint,
@TheQuarterName nvarchar(10),
@TheWeek smallint,
@TheWeekName nvarchar(10),
@Vacation_Mark smallint,
@dDate DATETIME,--存储起始日期和结束日期
@adddays smallint--存储日期增量
_select @adddays = 1 --日期增量
_select @dDate = '1/1/2000'--当前日期
WHILE @dDate <= '12/31/2010'--结束日期
BEGIN
set timeKey = cast((left(convert(nvarchar,thedate,23),4)+substring(convert(nvarchar,thedate,23),6,2)+
substring(convert(nvarchar,thedate,23),9,2))as int)
_select @TheDate = @dDate
_select @TheDateName = REPLACE(CONVERT(nvarchar(20),@dDate,111),'/','-')
_select @TheYear = DATENAME(yy, @dDate)
_select @TheYearName = CAST(@TheYear as nvarchar)+'年'
_select @TheMonth = DATENAME(mm, @dDate)
_select @TheMonthName =CAST(@TheMonth as nvarchar)+'月'
_select @TheDay = DATENAME(dd, @dDate)
_select @TheDayName = CAST(@TheDay as nvarchar)+'日'
_select @TheQuarter = DATENAME(Quarter, @dDate)
_select @TheQuarterName = '第' + CAST(DATENAME(Quarter, @dDate)as varchar(1))+'季度'
_select @TheWeek = DATEPART(dw, @dDate)
_select @TheWeekName = DATENAME(dw,@dDate)
_select @Vacation_Mark = CASE WHEN(@TheWeek = 1 OR @TheWeek = 7)THEN 1 ELSE 0 END
_insert INTO Dim_Date(TheDate,TheDateName,TheYear,TheYearName,TheMonth,TheMonthName,TheDay,
TheDayName,TheQuarter,TheQuarterName,TheWeek,TheWeekName,Vacation_Mark)VALUES
(@TheDate, @TheDateName,@TheYear,@TheYearName,@TheMonth,@TheMonthName,@TheDay,@TheDayName,@TheQuarter,
@TheQuarterName,@TheWeek,@TheWeekName,@Vacation_Mark)
_select @dDate = @dDate + @adddays
END
GO
|