SQL语句关键字UNION小知识

来源:互联网 发布:sqlserver 18456错误 编辑:程序博客网 时间:2024/06/07 21:16

   今天在用UNION进行将两个查询结合时,发现加了text类型的字段进去就报错了,错误信息如下:

不能以 DISTINCT 方式选择 text、ntext 或 image 数据类型。

 

     经过翻阅资料后才知道,原因在于如此操作 无法对text类型判断是否重复,所以要在UNION后加个ALL关键字,如下:

 

select top 6 * from
(select top 6 a.content,b.title, b.takenoticeid ID,b.taketime,type = 1 from takenotice_content as a,
(select * from comm_takenotice_201004 where takeperson = 'g567890' )as b
 where a.contentid = b.contentid and b.readstatus ='0'

union all

select top 6 a.content,title = '', b.takesmsid ID,b.taketime,type = 2  from takesms_content as a,
(select * from comm_takesms_201004 where takeperson = 'g567890')as b
 where a.contentid = b.contentid ) x order by taketime desc

 

 

   如需按某一条件排序,则要加别名,如上面sql中的x,方可执行。

原创粉丝点击