sql中 union all 急!!!!

来源:百度知道 编辑:UC知道 时间:2024/06/27 15:22:14
我想给
select * from tab1 union all select * from tab2
生成的新表起个别名,怎么做?

select *
into #table_1
from (select * from tab1 union all select * from tab2)

对吗?

这样写会报错:
改为:
select *
into #table_1
from (select * from tab1 union all select * from tab2) as tmp
上面建的是临时表

sqlserver的这样写:
insert into table_1 select *
from (select * from tab1 union all select * from tab2) as tmp

少个别名吧
select *
into #table_1
from (select * from tab1 union all select * from tab2) t

select * from (
select * from tab1
union all
select * from tab2

) as newTableName

----意思就是在你查出的表外围套上一层表,然后取别名

select *
into #table_1
from (select * from tab1 union all select * from tab2) as tmp
上面的tmp随便取的

上次我写这种语句的时候sql提醒说只要在第一个select查询中使用into就可以了,试过的确可以.我当时用的sql2005,不知道你的是否可以使用.

select * from (
select * from tab1 union all select * from tab2
) a