如何使用SELECT * INTO tempTable FROM CTE Query创buildTemp表
我有一个MS SQL CTE查询,我想从中创build一个临时表。 我不知道该怎么做,因为它提供了一个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, 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)
如果能从这个CTE查询中创build一个临时表,我将非常感激
样品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
真的格式可以很简单 – 有时不需要预先定义临时表 – 它将从select的结果创build。
Select FieldA...FieldN into #MyTempTable from MyTable
所以,除非你想要不同的types,或者对定义非常严格,否则保持简单。 还要注意,存储过程内部创build的任何临时表将在存储过程完成执行时自动删除。 如果存储过程A创build临时表并调用存储过程B,则B将能够使用A创build的临时表。
但是,通常认为良好的编码实践显式删除您创build的每个临时表。
SELECT ... INTO
需要从CTE中select。
;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)
如何在存储过程中使用TempTable?
这里是步骤:
创buildTEMP表
-- CREATE TEMP TABLE Create Table #MyTempTable ( EmployeeID int );
INSERT TEMP SELECT DATA INTO TEMP TABLE
-- INSERT COMMON DATA Insert Into #MyTempTable Select EmployeeID from [EmployeeMaster] Where EmployeeID between 1 and 100
SELECT TEMP TABLE(您现在可以使用此select查询)
Select EmployeeID from #MyTempTable
最后一步放下桌子
Drop Table #MyTempTable
我希望这将有所帮助。 简单而清晰:)