2011-03-24 2 views
1

안녕하세요,반복적으로 임시 테이블로 선택

반복적으로 같은 임시 테이블에 선택하고 전체 결과를 반환 할 수 있습니다.

뭔가

select 
    col1, 
    col2 
into #temp 
where 
    col4="abc" 
    and col5=10 
select 
    col1, 
    col10 
into #temp 
where 
    col4="dbe" 
    and col5=15 
select * from #temp 

처럼 나는 그것을 시도하고 그것은 단지 첫 번째 부분을 반환했습니다.

+0

오류가 발생했습니다. –

+0

UNION을 사용해 보셨습니까? – malinois

답변

4
/*First one creates the table*/ 
    select 
     col1, 
     col2 
    into #temp 
    from something 
    where 
     col4="abc" 
     and col5=10 

/*Now insert into the table that you just created*/  
    insert into #temp 
    select 
     col1, 
     col10 
    from something 
    where 
     col4="dbe" 
     and col5=15 
    select * from #temp 

또한 오류에서 생을 마감한다

select 
     col1, 
     col2 
    into #temp FROM (
    select 
     col1, 
     col2 
    from something  
    where 
     col4="abc" 
     and col5=10 
union all 
    select 
     col1, 
     col10 
    from something   
    where 
     col4="dbe" 
     and col5=15) derived 
+0

감사합니다. 이전에 노조에 대해 들어 본 적이 없으며, 임시 테이블이 실제로 필요하지 않습니다. – Charbel

+0

@Charbel - 아 임시 테이블의 목적이 결과를 통합하는 것이라면 확실히 UNION ALL '직접. –

0

위의 코드를 할 수 있습니다. 첫 번째 선택 후 입력해야합니다. 시험해보십시오.

select 
     col1, 
     col2 
    into #temp 
    where 
     col4="abc" 
     and col5=10; 
    insert into #temp 
    select 
     col1, 
     col10 
    where 
     col4="dbe" 
     and col5=15; 

    select * from #temp 
관련 문제