我有一个table_A
,其中包含一组列A1
,A2
和一个table_b,其中包含一组列B1
,B2
它发生在A2=B1
但其余列不匹配(并且不应该)。我想附上表格,以便使用UNION ALL
对于非匹配列,我在null as COLUMN_NAME
语句的两侧使用UNION
CREATE VIEW MY_VIEW AS
SELECT
TABLE_A.A1,
TABLE_A.A2,
null as B2
from TABLE_A
union all
SELECT
null as A1,
TABLE_B.B1 as A2,
TABLE_B.B2 as B2
from TABLE_B;
输出以下错误:
Error report: SQL Error: ORA-01790: expression must have same datatype as corresponding expression 01790. 00000 - "expression must have same datatype as corresponding expression"
是否因为空值?
答案 0 :(得分:7)
您需要将NULL显式地转换为上部SELECT
中的相应类型。
CREATE VIEW MY_VIEW AS
SELECT
TABLE_A.A1,
TABLE_A.A2,
CAST(null AS <type_of_TABLE_B_B2>) as B2
from TABLE_A
union all
SELECT
null,
TABLE_B.B1,
TABLE_B.B2
from TABLE_B;
至于替代方案,因为 @evilive 表示你可以使用固定值作为VARCHAR的空字符串(''
)或NUMBERs的零,但对于我的意见,显式转换是更好的解决方案,因为它是显而易见的,不会引起意外