读书人

sql 运用union出

发布时间: 2012-10-14 14:55:08 作者: rapoo

sql 使用union出

SQL code
select a,b,c into #tb from table union select a,b,c into #tb2 from table2


union前後的句都可以行,但是加上union一起的就;

提示:SELECT INTO must be the first query in a statement containing a UNION, INTERSECT or EXCEPT operator.



[解决办法]
SQL code
select a,b,c into #tb from table select a,b,c into #tb2 from table2select * from #tbunionselect * from #tb2
[解决办法]
select a,b,c into #tb from table 是创建表并且插入数据,并不是查询结果

而union 是取那个结果集的并集 ,所以不能这样写
[解决办法]
SQL code
select a,b,c into #tb from table select a,b,c into #tb2 from table2select * from #tbunion all --不过滤重复数据了select * from #tb2
[解决办法]
不能带着into 进行 union。

读书人网 >SQL Server

热点推荐