如何使用SELECT * INTO temp表FROM CTE Query创建Temp表

时间:2012-07-15 10:45:41

标签: sql sql-server common-table-expression

我有一个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, 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查询创建临时表

7 个答案:

答案 0 :(得分:199)

样本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

答案 1 :(得分:117)

真的格式可以非常简单 - 有时候不需要预定义临时表 - 它将根据select的结果创建。

Select FieldA...FieldN 
into #MyTempTable 
from MyTable

因此,除非您需要不同类型或对定义非常严格,否则请保持简单。另请注意,在存储过程完成执行时,将自动删除在存储过程中创建的任何临时表。如果存储过程A创建临时表并调用存储过程B,则B将能够使用A创建的临时表。

但是,通常认为良好的编码习惯是明确删除您创建的每个临时表。

答案 2 :(得分:22)

SELECT ... INTO需要在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) 

答案 3 :(得分:19)

如何在存储过程中使用TempTable?

以下是步骤:

创建TEMP表

-- CREATE TEMP TABLE 
Create Table #MyTempTable (
    EmployeeID int
);

将TEMP SELECT DATA数据插入TEMP表

-- INSERT COMMON DATA
Insert Into #MyTempTable
Select EmployeeID from [EmployeeMaster] Where EmployeeID between 1 and 100

SELECT TEMP TABLE(您现在可以使用此选择查询)

Select EmployeeID from #MyTempTable

最后一步降低表

Drop Table #MyTempTable

我希望这会有所帮助。简单明了:)

答案 4 :(得分:2)

以下是对在执行后创建表的查询的答案的一个小改动(即,您不必先创建表):

SELECT * INTO #Temp
FROM (
select OptionNo, OptionName from Options where OptionActive = 1
) as X

答案 5 :(得分:1)

Select      Eventname, 
            count(Eventname) as 'Counts'
INTO        #TEMPTABLE                                                                                
FROM        tblevent
where       Eventname like 'A%'
Group by    Eventname
order by    count(Eventname)

这里使用into子句直接创建表

答案 6 :(得分:0)

也可以使用View来创建临时/虚拟表 CREATE VIEW view_name AS 选择第 1 列、第 2 列、... 从表名 WHERE 条件;