복붙노트

[SQL] CTE 쿼리에서 SELECT * INTO 임시 테이블에 임시 테이블을 만드는 방법

SQL

CTE 쿼리에서 SELECT * INTO 임시 테이블에 임시 테이블을 만드는 방법

나는 임시 테이블을 만들에서 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 쿼리에서 임시 테이블을 만들 수 있는지 올바른 방향으로 한 점을 감사하거나 것

해결법

  1. ==============================

    1.

    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
    
  2. ==============================

    2.정말 형식은 매우 간단 할 수있다 - 때로는 임시 테이블을 미리 정의 할 필요가 없습니다 - 그것은 선택의 결과에서 생성됩니다.

    정말 형식은 매우 간단 할 수있다 - 때로는 임시 테이블을 미리 정의 할 필요가 없습니다 - 그것은 선택의 결과에서 생성됩니다.

    Select FieldA...FieldN 
    into #MyTempTable 
    from MyTable
    

    당신은 다른 유형을 원하거나 정의에 매우 엄격하지 않는 그래서, 일을 간단하게 유지한다. 저장 프로 시저의 완료를 실행할 때 참고는 저장 프로 시저 안에 생성 된 임시 테이블이 자동으로 삭제된다. 저장 프로 시저 A는 임시 테이블 및 전화 저장 프로 시저 B를 생성하면, B는 A가 만든 임시 테이블을 사용할 수 있습니다.

    그러나, 일반적으로 좋은 명시 적으로 어쨌든 만들 때마다 임시 테이블을 드롭 코딩 연습 간주됩니다.

  3. ==============================

    3.선택은 ... INTO는 CTE에서 선택 될 필요가있다.

    선택은 ... 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) 
    
  4. ==============================

    4.어떻게 저장 프로 시저에서 임시 테이블을 사용하는 방법?

    어떻게 저장 프로 시저에서 임시 테이블을 사용하는 방법?

    다음 단계는 다음과 같습니다

    TEMP 테이블을 생성

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

    INSERT의 TEMP SELECT 데이터로의 임시 테이블

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

    SELECT 임시 테이블은 (이제이 선택 쿼리를 사용할 수 있습니다)

    Select EmployeeID from #MyTempTable
    

    마지막 단계 드롭 표

    Drop Table #MyTempTable
    

    나는이 의지의 도움을 바랍니다. 간단하고 명확한 :)

  5. ==============================

    5.

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

    여기에 절을 사용하여 테이블을 직접 작성

  6. ==============================

    6.다음 실행시 테이블을 생성하는 쿼리의 응답 한 약간의 변경은 (즉, 먼저 테이블을 만들 필요가 없습니다)입니다 :

    다음 실행시 테이블을 생성하는 쿼리의 응답 한 약간의 변경은 (즉, 먼저 테이블을 만들 필요가 없습니다)입니다 :

    SELECT * INTO #Temp
    FROM (
    select OptionNo, OptionName from Options where OptionActive = 1
    ) as X
    
  7. from https://stackoverflow.com/questions/11491240/how-to-create-temp-table-with-select-into-temptable-from-cte-query by cc-by-sa and MIT license