开发者

select into temporary table repeatedly

开发者 https://www.devze.com 2023-02-19 05:43 出处:网络
HI, how can I select repeatedly into the same temporary table and then return the full outcome. something like

HI,

how can I select repeatedly into the same temporary table and then return the full outcome.

something like

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
开发者_StackOverflow中文版

I tried it, and it returned only the first part.


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

You could also do

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


The above code should end up in error. You should insert into after the first select into. Try this:

 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
0

精彩评论

暂无评论...
验证码 换一张
取 消