I am building a Data warehouse for my school project. I have already created my time dimension table and it works.
` CREATE TABLE TimeDim(
[timeCode] int primary key,
[date] datetime,
[year] char(4),
[month] varchar(2),
[monthName] varchar(9),
[Quarter] varchar(9),
[dayOfMonth] varchar(2),
[dayName] varchar(9),
[dayOfQuarter] varchar(3),
[dayOfYear] varchar(3),
[weekOfMonth] varchar(1),
[weekOfQuarter] varchar(2),
[weekOfYear] varchar(2));`
However, the errors occur when I try to create data in the Time Dimension table.The error shows: **
`Msg 213, Level 16, State 1, Line 95
Column name or number of supplied values does not match table definition.`
** The code for me to create data is
DECLARE @StartDate DATETIME = '01/01/2003' --Starting value of Date Range
DECLARE @EndDate DATETIME = '01/01/2006' --End Value of Date Range ( end at 31/12/1999)
--Temporary Variables To Hold the Values During Processing of Each Date of Year
DECLARE
@DayOfWeekInMonth INT,
@DayOfWeekInYear INT,
@DayOfQuarter INT,
@WeekOfMonth INT,
@CurrentYear INT,
@CurrentMonth INT,
@CurrentQuarter INT
/*Table Data type to store the day of week count for the month and year*/
DECLARE @DayOfWeek TABLE (DOW INT, MonthCount INT, QuarterCount INT, YearCount INT)
INSERT INTO @DayOfWeek VALUES (1, 0, 0, 0)
INSERT INTO @DayOfWeek VALUES (2, 0, 0, 0)
INSERT INTO @DayOfWeek VALUES (3, 0, 0, 0)
INSERT INTO @DayOfWeek VALUES (4, 0, 0, 0)
INSERT INTO @DayOfWeek VALUES (5, 0, 0, 0)
INSERT INTO @DayOfWeek VALUES (6, 0, 0, 0)
INSERT INTO @DayOfWeek VALUES (7, 0, 0, 0)
--Extract and assign various parts of Values from Current Date to Variable
DECLARE @CurrentDate AS DATETIME = @StartDate
SET @CurrentMonth = DATEPART(MM, @CurrentDate)
SET @CurrentYear = DATEPART(YY, @CurrentDate)
SET @CurrentQuarter = DATEPART(QQ, @CurrentDate)
/********************************************************************************************/
--Proceed only if Start Date(Current date ) is less than End date you specified above
WHILE @CurrentDate < @EndDate
BEGIN
/*Begin day of week logic*/
/*Check for Change in Month of the Current date if Month changed then
Change variable value*/
IF @CurrentMonth != DATEPART(MM, @CurrentDate)
BEGIN
UPDATE @DayOfWeek
SET MonthCount = 0
SET @CurrentMonth = DATEPART(MM, @CurrentDate)
END
/* Check for Change in Quarter of the Current date if Quarter changed then change
Variable value*/
IF @CurrentQuarter != DATEPART(QQ, @CurrentDate)
BEGIN
UPDATE @DayOfWeek
SET QuarterCount = 0
SET @CurrentQuarter = DATEPART(QQ, @CurrentDate)
END
/* Check for Change in Year of the Current date if Year changed then change
Variable value*/
IF @CurrentYear != DATEPART(YY, @CurrentDate)
BEGIN
UPDATE @DayOfWeek
SET YearCount = 0
SET @CurrentYear = DATEPART(YY, @CurrentDate)
END
-- Set values in table data type created above from variables
UPDATE @DayOfWeek
SET
MonthCount = MonthCount + 1,
QuarterCount = QuarterCount + 1,
YearCount = YearCount + 1
WHERE DOW = DATEPART(DW, @CurrentDate)
SELECT
@DayOfWeekInMonth = MonthCount,
@DayOfQuarter = QuarterCount,
@DayOfWeekInYear = YearCount
FROM @DayOfWeek
WHERE DOW = DATEPART(DW, @CurrentDate)
/*End day of week logic*/
/* Populate Your Dimension Table with values*/
INSERT INTO [dbo].[TimeDim]
SELECT
CONVERT (char(8),@CurrentDate,112) as timeCode,
@CurrentDate AS date,
CONVERT (char(10),@CurrentDate,103) as FullDateUK,
CONVERT (char(10),@CurrentDate,101) as FullDateUSA,
DATEPART(DD, @CurrentDate) AS dayOfMonth,
--Apply Suffix values like 1st, 2nd 3rd etc..
DATENAME(DW, @CurrentDate) AS dayName,
@DayOfQuarter AS dayOfQuarter,
DATEPART(DY, @CurrentDate) AS dayOfYear,
DATEPART(WW, @CurrentDate) + 1 - DATEPART(WW, CONVERT(VARCHAR,
DATEPART(MM, @CurrentDate)) + '/1/' + CONVERT(VARCHAR,
DATEPART(YY, @CurrentDate))) AS weekOfMonth,
(DATEDIFF(DD, DATEADD(QQ, DATEDIFF(QQ, 0, @CurrentDate), 0),
@CurrentDate) / 7) + 1 AS weekOfQuarter,
DATEPART(WW, @CurrentDate) AS weekOfYear,
DATEPART(MM, @CurrentDate) AS month,
DATENAME(MM, @CurrentDate) AS monthName,
DATEPART(QQ, @CurrentDate) AS Quarter,
DATEPART(YEAR, @CurrentDate) AS year
SET @CurrentDate = DATEADD(DD, 1, @CurrentDate)
END
The Error is so clear:-
Column name or number of supplied values does not match table definition.
That means you trying insert number of values doesn't match with number of columns.
The number of Columns is 13 , as next:-
The number of values is 36 , as following:-
After Editing the script , the error still existing, because the number of values is 15 as following:-
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.