1.创建三个实验用表T1、T2和T3
sec@ora10g> create table t1 (x varchar2(10), y varchar2(10));
Table created.
sec@ora10g> create table t2 (x varchar2(10), y varchar2(10));
Table created.
sec@ora10g> create table t3 (x int, y varchar2(10));
Table created.
注意T1和T2表的所有字段类型均为“VARCHAR2”,T3表的X字段类型是“INT”。
2.每张表初始化一条记录
sec@ora10g> insert into t1 values ('1', 'secooler');
1 row created.
sec@ora10g> insert into t2 values ('2','HOU');
1 row created.
sec@ora10g> insert into t3 values (3,'Andy');
1 row created.
sec@ora10g> commit;
Commit complete.
3.先看正确的使用方法
sec@ora10g> select * from t1
2 union all
3 select * from t2;
X Y
---------- ----------
1 secooler
2 HOU
由于T1表和T2表的字段类型相同,故此处可以得到正确的结果。
4.使用T2和T3表来模拟ORA-01790错误
sec@ora10g> select * from t1
2 union all
3 select * from t3;
select * from t1
*
ERROR at line 1:
ORA-01790: expression must have same datatype as corresponding expression
5.规避ORA-01790错误的一种方法
针对这个案例,这里我们采用TO_CHAR函数强制将T3表的X列转换为字符串类型的方法来处理。
sec@ora10g> select * from t1
2 union all
3 select to_char(x),y from t3;
X Y
---------------------------------------- ----------
1 secooler
3 Andy
6.MOS中的参考信息
[ID 19128.1]
OERR: ORA 1790 expression must have same datatype as corresponding expression
Error: ORA 1790
Text: expression must have same datatype as corresponding expression
-------------------------------------------------------------------------------
Cause: A SELECT list item corresponds to a SELECT list item with a different
datatype in another query of the same set expression.
Action: Check that all corresponding SELECT list items have the same datatypes.
Use the TO_NUMBER, TO_CHAR, and TO_DATE functions to do explicit data
conversions.