首页 > 解决方案 > 使用select语句将数据插入临时表?

问题描述

我想从 select 语句中手动将数据插入到临时表中:

  Select into #temp from (select 1,2
    Union 
    select 2,4 
    Union 
    Select 8,12) as b

标签: sql-serverdatabasesql-inserttemp-tables

解决方案


您需要为列命名(这里我已命名列ab):

Select a, b into #temp 
from 
(
     select a = 1, b = 2 
     Union 
     select 2, 4 
     Union 
     Select 8, 12
) as t

select * from #temp

a   b
-----
1   2
2   4
8   12

只有 a 的第一个SELECT子句UNION需要明确的列名。


推荐阅读