如何让我的SQL解码工作

时间:2014-04-04 19:40:18

标签: sql oracle union decode

Hello是否有另一种方法可以绕过这个错误,它抱怨我的一个解码语句。所以我想要的是我的子查询中的access_ind显示TA500-CO = 12; TA5006-HDT = 14,TA5004-HDT = 13.

 DECODE (b.cfg_type, 'TA5000-CO', '12', 'TA5006-HDT', '14', 'TA5004-HDT', '13') access_ind.

我得到一个ORA-01790:表达式必须与相应的表达式具有相同的数据类型。这是我下面的SQL查询。

 SELECT   a.eid, a.node_sid, a.sw_version,  
             DECODE (a.cfg_type,  
                     '7330FTTN', 'Alcatel 7330',  
                     '7340FTTU', 'Alcatel 7340',  
                     '7342GPON', 'Alcatel 7342'  
                    ) cfg_type,  
             a.ems_name,  
             DECODE (a.ems_ip_addr, 'Unknown', NULL, a.ems_ip_addr) ems_ip_addr,  
             a.admin_state,  
             nvl(r.access_ind, 0) access_ind 
        FROM actl73x0 a, RPT_FTTX_ELEMENT_ID r  
       WHERE r.eid (+) = a.eid  
     UNION  
     SELECT   b.eid, b.node_sid, b.sw_version,  
             DECODE (b.cfg_type, 'BLM1500', 'Ericsson 1500', 'Ericsson 1500') cfg_type,  
             b.ems_name,  
             DECODE (b.ems_ip_addr, 'Unknown', NULL, b.ems_ip_addr) ems_ip_addr,  
             b.admin_state, 6 access_ind  
        FROM blm b  

     UNION  
     SELECT   b.eid, b.node_sid, b.sw_version,  
             DECODE (b.cfg_type, 'TA5000-CO', 'TA5006-HDT', 'TA5004-HDT') cfg_type,  
             b.ems_name,  
             DECODE (b.ems_ip_addr, 'Unknown', NULL, b.ems_ip_addr) ems_ip_addr,  
             b.admin_state, 
             DECODE (b.cfg_type, 'TA5000-CO', '12', 'TA5006-HDT', '14', 'TA5004-HDT', '13') access_ind    
        FROM ecil_ta500x b  
     UNION  
     SELECT   e.eid, e.node_sid, e.sw_version,  
             DECODE (e.cfg_type, 'EDA1200', 'EDA 1200', 'EDA 1200') cfg_type,  
             e.ems_name,  
             DECODE (e.ems_ip_addr, 'Unknown', NULL, e.ems_ip_addr) ems_ip_addr,  
              NULL admin_state, 
              nvl(r.access_ind, 0) access_ind 
        FROM eda e, RPT_FTTX_ELEMENT_ID r  
       WHERE r.eid (+) = e.eid  
     ORDER BY 1  ;

1 个答案:

答案 0 :(得分:2)

union 语句的

ORA-01790 表示您的工会的所有相应列必须具有相同的列类型:

select a from taba
union
select b from tabb

此处ab必须属于同一类型。

在您的所有子选择中,除了使用您的解码的列之外,您的access_ind列类型为number

DECODE (b.cfg_type, 'TA5000-CO', '12', 'TA5006-HDT', '14', 'TA5004-HDT', '13') access_ind

此处返回varchar值。这就是导致错误。将此行更改为

DECODE (b.cfg_type, 'TA5000-CO', 12, 'TA5006-HDT', 14, 'TA5004-HDT', 13) access_ind

它应该有效。在这里,我使用12代替'12',依此类推。