How to create Temp table with SELECT * INTO tempTable FROM CTE Query

前端 未结 6 1293
执笔经年
执笔经年 2020-12-04 05:39

I have a MS SQL CTE query from which I want to create a temporary table. I am not sure how to do it as it gives an Invalid Object name error.

Below is t

6条回答
  •  误落风尘
    2020-12-04 06:00

    The SELECT ... INTO needs to be in the select from the CTE.

    ;WITH Calendar
         AS (SELECT /*... Rest of CTE definition removed for clarity*/)
    SELECT EventID,
           EventStartDate,
           EventEndDate,
           PlannedDate                   AS [EventDates],
           Cast(PlannedDate AS DATETIME) AS DT,
           Cast(EventStartTime AS TIME)  AS ST,
           Cast(EventEndTime AS TIME)    AS ET,
           EventTitle,
           EventType
    INTO TEMPBLOCKEDDATES /* <---- INTO goes here*/        
    FROM   Calendar
    WHERE  ( PlannedDate >= Getdate() )
           AND ',' + EventEnumDays + ',' LIKE '%,' + Cast(Datepart(dw, PlannedDate) AS CHAR(1)) + ',%'
            OR EventEnumDays IS NULL
    ORDER  BY EventID,
              PlannedDate
    OPTION (maxrecursion 0) 
    

提交回复
热议问题