Fork me on GitHub

ORA-01790 错误处理

今天在练手的时候出现了一个ORA-01790 的错误,决定把他写下来保留起来。
先来创建两张测试用的简单的表。
SQL> create table test01 (id number(3),name varchar2(12));
Table created.
SQL> create table test02 (id varchar2(6),name varchar2(12));
Table created.
分别插入一条记录用来测试。
SQL> insert into test01 values (100,'baidu');
1 row created.
SQL> insert into test02 values ('101','sina');
1 row created.
SQL> commit;
Commit complete.
执行带union 或者union all 的语句。
SQL> select id,name from test01
  2  union 
  3  select id,name from test02;
select id,name from test01
       *
ERROR at line 1:
ORA-01790: expression must have same datatype as corresponding expression
出现上述错误的原因是因为 test01 中的id 列的定义是number,而test02 中id 列的定义
是varchar2。所以在union 或者union all 的时候造成了数据类型不一致。出现上述错误应该根据不同的情况使用不同类型转换函数,比如to_char,to_number,to_date
改写上面的语句:
SQL> select to_char(id) as id,name from test01
  2  union 
  3  select id,name from test02;
ID                                       NAME
---------------------------------------- ------------
100                                      baidu
101                                      sina

 

posted @ 2016-07-14 11:11  ZZZZW  阅读(3141)  评论(0编辑  收藏  举报
AmazingCounters.com