Reputation: 1
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 the whole query for reference
SELECT * INTO TEMPBLOCKEDDATES FROM
;with Calendar as (
select EventID, EventTitle, EventStartDate, EventEndDate, EventEnumDays,EventStartTime,EventEndTime, EventRecurring, EventStartDate as PlannedDate
,EventType from EventCalender
where EventActive = 1 AND LanguageID =1 AND EventBlockDate = 1
union all
select EventID, EventTitle, EventStartDate, EventEndDate, EventEnumDays,EventStartTime,EventEndTime, EventRecurring, dateadd(dd, 1, PlannedDate)
,EventType from Calendar
where EventRecurring = 1
and dateadd(dd, 1, PlannedDate) <= EventEndDate
)
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 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)
I would appreciate a point in the right direction or if I can create a temporary table from this CTE query
Upvotes: 0
Views: 942
Reputation: 1269743
The correct syntax looks like this:
Below is the whole query for reference
with Calendar as (
select EventID, EventTitle, EventStartDate, EventEndDate, EventEnumDays,
EventStartTime, EventEndTime, EventRecurring, EventStartDate as PlannedDate,
EventType
from EventCalender
where EventActive = 1 AND LanguageID =1 AND EventBlockDate = 1
union all
select EventID, EventTitle, EventStartDate, EventEndDate, EventEnumDays,
EventStartTime, EventEndTime, EventRecurring, dateadd(day, 1, PlannedDate),
EventType
from Calendar
where EventRecurring = 1 and
dateadd(day, 1, PlannedDate) <= EventEndDate
)
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
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);
The ordering of clauses is important:
with
goes before select
.where
probably doesn't do what you want. But I'm leaving your original logic. (I'm guessing you should have parentheses.)Upvotes: 1