sql 使用union出現問題

chinagzu 2012-09-26 10:05:37
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.

...全文
107 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
chinagzu 2012-09-26
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
SQL code

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

select * from #tb
union all --不过滤重复数据了
select * from #tb2
[/Quote]

OK就是這樣子的,謝謝樓上兩個高手。
叶子 2012-09-26
  • 打赏
  • 举报
回复
不能带着into 进行 union。

ttpsan550 2012-09-26
  • 打赏
  • 举报
回复

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

select * from #tb
union all --不过滤重复数据了
select * from #tb2
chinagzu 2012-09-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
SQL code

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

select * from #tb
union
select * from #tb2
[/Quote]

謝謝您的回答但這樣會報錯:The ntext data type cannot be selected as DISTINCT because it is not comparable。謝謝。
ttpsan550 2012-09-26
  • 打赏
  • 举报
回复
select a,b,c into #tb from table 是创建表并且插入数据,并不是查询结果

而union 是取那个结果集的并集 ,所以不能这样写
--小F-- 2012-09-26
  • 打赏
  • 举报
回复

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

select * from #tb
union
select * from #tb2

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧