How can I generate a temporary table filled with dates in SQL Server 2000?

后端 未结 11 561
没有蜡笔的小新
没有蜡笔的小新 2020-11-30 05:21

I need to make a temporary table that holds of range of dates, as well as a couple of columns that hold placeholder values (0) for future use. The dates I need are the firs

11条回答
  •  天涯浪人
    2020-11-30 05:56

    I needed something similar, but all DAYS instead of all MONTHS.

    Using the code from MatBailie as a starting point, here's the SQL for creating a permanent table with all dates from 2000-01-01 to 2099-12-31:

    CREATE TABLE _Dates (
      d DATE,
      PRIMARY KEY (d)
    )
    DECLARE @dIncr DATE = '2000-01-01'
    DECLARE @dEnd DATE = '2100-01-01'
    
    WHILE ( @dIncr < @dEnd )
    BEGIN
      INSERT INTO _Dates (d) VALUES( @dIncr )
      SELECT @dIncr = DATEADD(DAY, 1, @dIncr )
    END
    

提交回复
热议问题