如何解决如何使用 SELECT * INTO tempTable FROM CTE 查询创建临时表
示例 DDL
create table #Temp
(
EventID int,
EventTitle Varchar(50),
EventStartDate DateTime,
EventEndDate DatetIme,
EventEnumDays int,
EventStartTime Datetime,
EventEndTime DateTime,
EventRecurring Bit,
EventType int
)
;WITH Calendar
AS (SELECT /*...*/)
Insert Into #Temp
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
确保表在使用后被删除
If(OBJECT_ID('tempdb..#temp') Is Not Null)
Begin
Drop Table #Temp
End
解决方法
我有一个 MS SQL CTE 查询,我想从中创建一个临时表。我不知道该怎么做,因为它给出了一个Invalid Object name
错误。
以下是整个查询以供参考
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,dateadd(dd,1,PlannedDate),EventType from Calendar
where EventRecurring = 1
and dateadd(dd,PlannedDate) <= EventEndDate
)
select EventID,PlannedDate as [EventDates],Cast(PlannedDate As datetime) AS DT,Cast(EventStartTime As time) AS ST,Cast(EventEndTime As time) AS ET,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)
如果我能从这个 CTE 查询中创建一个临时表,我将不胜感激
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。