重复选择临时表

发布于 2024-10-26 08:27:11 字数 278 浏览 1 评论 0原文

您好,

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

就像

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

我尝试过的那样,它只返回了第一部分。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少跟Wǒ拽 2024-11-02 08:27:16

上面的代码应该会出错。
您应该在第一个 select into 之后插入。
试试这个:

 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

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
烙印 2024-11-02 08:27:15
    /*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
    /*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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文