简体   繁体   中英

SQL can't insert data for time dimension table

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:-

  1. timeCode
  2. date
  3. year
  4. month
  5. monthName
  6. Quarter
  7. dayOfMonth
  8. dayName
  9. dayOfQuarter
  10. dayOfYear
  11. weekOfMonth
  12. weekOfQuarter
  13. weekOfYear

The number of values is 36 , as following:-

  1. TimeKey
  2. Date
  3. FullDateUK
  4. FullDateUSA
  5. DayOfMonth
  6. DaySuffix
  7. DayName
  8. DayOfWeekUSA
  9. DayOfWeekUK
  10. DayOfWeekInMonth
  11. DayOfWeekInYear
  12. DayOfQuarter
  13. DayOfYear
  14. WeekOfMonth
  15. WeekOfQuarter
  16. WeekOfYear
  17. Month
  18. MonthName
  19. MonthOfQuarter
  20. Quarter
  21. QuarterName
  22. Year
  23. YearName
  24. MonthYear
  25. MMYYYY
  26. FirstDayOfMonth
  27. LastDayOfMonth
  28. FirstDayOfQuarter
  29. LastDayOfQuarter
  30. FirstDayOfYear
  31. LastDayOfYear
  32. IsHolidayUSA
  33. IsWeekday
  34. HolidayUSA
  35. (No column name)
  36. (No column name)

UPDATE:-

After Editing the script , the error still existing, because the number of values is 15 as following:-

  1. timeCode
  2. date
  3. FullDateUK
  4. FullDateUSA
  5. dayOfMonth
  6. dayName
  7. dayOfQuarter
  8. dayOfYear
  9. weekOfMonth
  10. weekOfQuarter
  11. weekOfYear
  12. month
  13. monthName
  14. Quarter
  15. year

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM