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

后端 未结 11 544
没有蜡笔的小新
没有蜡笔的小新 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:54

    Tested below and it works, though it's a bit convoluted.

    I assigned arbitrary values to the dates for the test.

    DECLARE @SD smalldatetime,
            @ED smalldatetime,
            @FD smalldatetime,
            @LD smalldatetime,
            @Mct int,
            @currct int = 0
    
    SET @SD = '1/15/2011'
    SET @ED = '2/02/2012'
    
    
    SET @FD = (DATEADD(dd, -1*(Datepart(dd, @SD)-1), @sd))
    SET @LD = (DATEADD(dd, -1*(Datepart(dd, @ED)-1), @ED))
    
    SET @Mct = DATEDIFF(mm, @FD, @LD)
    
    CREATE TABLE #MyTempTable (FoM smalldatetime, Trials int, Sales money)
    
    WHILE @currct <= @Mct
    BEGIN
        INSERT INTO #MyTempTable (FoM, Trials, Sales)
        VALUES
        (DATEADD(MM, @currct, @FD), 0, 0)
        SET @currct = @currct + 1
    END
    
    
    SELECT * FROM #MyTempTable
    
    DROP TABLE #MyTempTable
    

提交回复
热议问题