215

我有一个 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 查询中创建一个临时表,我将不胜感激

4

7 回答 7

289

示例 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
于 2012-07-15T11:07:36.403 回答
218

实际上,格式可以非常简单——有时不需要预定义临时表——它将根据选择的结果创建。

Select FieldA...FieldN 
into #MyTempTable 
from MyTable

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

但是,无论如何,显式删除您创建的每个临时表通常被认为是良好的编码习惯。

于 2014-01-06T01:50:38.090 回答
37

如何在存储过程中使用 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

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

于 2017-11-06T20:13:06.903 回答
29

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) 
于 2012-07-15T10:54:45.307 回答
11

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

SELECT * INTO #Temp
FROM (
select OptionNo, OptionName from Options where OptionActive = 1
) as X
于 2020-04-20T22:16:32.907 回答
9
Select      Eventname, 
            count(Eventname) as 'Counts'
INTO        #TEMPTABLE                                                                                
FROM        tblevent
where       Eventname like 'A%'
Group by    Eventname
order by    count(Eventname)

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

于 2018-05-10T09:46:23.437 回答
0

您还可以使用 View 创建临时/虚拟表 CREATE VIEW view_name AS SELECT column1, column2, ... FROM table_name WHERE 条件;

于 2021-06-04T12:30:04.193 回答