Sql 重复选择进入临时表

Sql 重复选择进入临时表,sql,select,Sql,Select,嗨 如何重复选择同一临时表,然后返回完整结果 差不多 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 我试过了,结果只返回了第一部分 /*First one creates the table

如何重复选择同一临时表,然后返回完整结果

差不多

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
我试过了,结果只返回了第一部分

    /*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
你也可以这样做

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

上面的代码应该以错误告终。 您应该在第一次选择“插入”之后插入到。 试试这个:

 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

上面的代码应该以错误告终。 您应该在第一次选择“插入”之后插入到。 试试这个:

 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

你应该从中得到一个错误。你应该从中得到一个错误。非常感谢,我以前从未听说过union,听起来不错,不需要临时工table@Charbel-啊,如果临时表的目的只是合并结果,那么您肯定应该直接使用
UNION ALL
。非常感谢,我以前从未听说过工会,听起来是个不错的选择,不需要临时工table@Charbel-啊,如果临时表的目的只是合并结果,那么您肯定应该直接使用
UNION ALL