当前位置: 代码迷 >> Sybase >> 一个关于union的有关问题
  详细解决方案

一个关于union的有关问题

热度:2152   发布时间:2013-02-26 00:00:00.0
一个关于union的问题
select   *   from   corp
where   cp_name   like   '%% '  
  and
cp_code   in    
  (select     ga_corp   from   gstacc   a,   account   b  
      where   a.ga_accno   =   b.ac_accno   and   b.ac_ctime   > '2007-02-28   23:59:20 '     and   b.ac_ctime   <=   '2007-03-31   23:30:25 '
      union  
select     ga_corp   from   htlhis..gstacc   ha,   htlhis..account   hb    
where   ha.ga_accno   =   hb.ac_accno   and   hb.ac_ctime   > '2007-02-28   23:59:20 '    
    and   hb.ac_ctime   <=   '2007-03-31   23:30:25 '   )  

我使用Union时,提示不能使用
问解决方式


------解决方案--------------------------------------------------------
select
*
from
corp
where
cp_name like '%% '
and
(cp_code in (select ga_corp from gstacc a, account b
where a.ga_accno = b.ac_accno and b.ac_ctime > '2007-02-28 23:59:20 ' and b.ac_ctime <= '2007-03-31 23:30:25 ')
or
(cp_code in (select ga_corp from htlhis..gstacc ha, htlhis..account hb
where ha.ga_accno = hb.ac_accno and hb.ac_ctime > '2007-02-28 23:59:20 '
and hb.ac_ctime <= '2007-03-31 23:30:25 '))
------解决方案--------------------------------------------------------
你的结果集结构不同。select * from corp select ga_corp from 你让两个结果集结构相同。还有哥们,sybase没有oracle那么智能。所有你的sql的质量只能靠你自己控制。你的sql有问题啊,尽量不要用like “%%” .建议看看sargs的规则。
------解决方案--------------------------------------------------------
用union all时,要确保两个表的列数,对应列的数据类型相同,系统自动以第一个查询的列名作为结果的列名。
------解决方案--------------------------------------------------------
个人建议用存储过程来实现,将你SQL语句进行拆分,并将结果放入临时表,最后返回临时表。