我有一个MS SQL CTE查询,从中我想创建一个临时表。我不知道如何做到这一点,因为它给出了一个无效对象名称错误。

下面是整个查询,供参考

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查询创建一个临时表,我将非常感激一个正确的方向


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) 

示例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 FieldA...FieldN 
into #MyTempTable 
from MyTable

所以,除非你想要不同的类型,或者对定义有非常严格的要求,否则请保持简单。还要注意,在存储过程中创建的任何临时表都会在存储过程执行完成时自动删除。如果存储过程A创建了一个临时表并调用存储过程B,那么B将能够使用存储过程A创建的临时表。

但是,通常认为良好的编码实践是显式地删除您创建的每个临时表。


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

以下是步骤:

创建临时表

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

将临时数据插入临时表

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

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


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

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


以下是对在执行时创建表的查询的答案的一个轻微更改(即你不必先创建表):

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

您还可以使用View创建一个临时/虚拟表 创建视图view_name AS SELECT columnn1, column2,… 从table_name 条件;


您还应该注意到存在GLOBAL临时表。这些可以从另一个连接引用。它们的行为与早期答案所描述的基本一致。要创建一个表,只需在表名前面加上##。这就是我一直在寻找的;希望其他降落在这里的人也能发现这一点。