我得到一个ORA-01790:表达式必须与相应的表达式错误消息具有相同的数据类型。下面是我正在使用的sql。 CASE声明是否导致此问题?我是sql的新手,这是我第一次尝试使用UNION语句。我尝试过连接而不是UNION,但我尝试过的任何连接都会导致prior_amt字段为空。谢谢你的帮助........
SELECT
pa.BUSINESS_UNIT as BUS_UNIT,
pa.DESCR AS DESCRIPT,
pdr.DEPTID AS DEPTID,
pdr.ASSET_ID AS ASSET_NO,
pdr.ACCOUNT_AD AS ACCT_AD,
pdr.BOOK AS BOOK,
MAX(CASE WHEN (pdr.FISCAL_YEAR =2014 AND pdr.ACCOUNTING_PERIOD =11) THEN pdr.DEPR END) as CURRENT_AMT,
MAX(CASE WHEN (pdr.FISCAL_YEAR =2104 AND pdr.ACCOUNTING_PERIOD =10) THEN pdr.DEPR END) as PRIOR_AMT,
'' AS ACCT_DE,
'' AS JRNL_ID,
'' AS JRNL_DT
FROM PS_ASSET pa
INNER JOIN PS_DEPR_RPT pdr
ON pa.ASSET_ID = pdr.ASSET_ID
AND pa.BUSINESS_UNIT = pdr.BUSINESS_UNIT
WHERE
pa.BUSINESS_UNIT='A0465'
AND pdr.BOOK='PERFORM'
AND ((pdr.FISCAL_YEAR=2014 AND pdr.ACCOUNTING_PERIOD=11)
OR (pdr.FISCAL_YEAR=2014 AND pdr.ACCOUNTING_PERIOD=10))
group by
pa.business_unit,
pa.descr,
pdr.deptid,
pdr.asset_id,
pdr.account_ad,
pdr.book
UNION ALL
select
'' as BUS_UNT,
'' AS DESCRIPT,
'' AS DEPTID,
'' AS ACCT_AD,
'' AS BOOK,
'' AS CURRENT_AMT,
'' AS PRIOR_AMT,
pdl.asset_id AS ASSET_NO,
pdl.account AS ACCT_DE,
pdl.journal_id AS JRNL_ID,
pdl.journal_date AS JRNL_DT
from ps_dist_ln pdl
where
book = 'PERFORM'
and business_unit = 'A0465'
and fiscal_year = 2014
and accounting_period = 11
and distribution_type = 'DE'
答案 0 :(得分:15)
您的问题是您在联盟的每一半中都以不同的顺序放置了列。列必须以相同的顺序在两半之间匹配。它与CASE表达无关。
此外,在您撰写2104的地方,可能应该是2014年。
答案 1 :(得分:0)
我遇到了这种错误的奇怪情况:
我在特定行收到了 ORA-01790: expression must have same datatype as corresponding expression
错误消息。
示例:
INSERT INTO TABLE_TO_INSERT (
COL_FIELD_1
,COL_FIELD_2
,COL_FIELD_3
,COL_FIELD_4
,COL_FIELD_5
,COL_FIELD_6
)
SELECT 1
,2
,'IMPORT'
,'EXPORT'
,COL_DEXPIRATION ---- > SQL Developer shows ORA-01790 at this error line.
,COL_DEXPIRATION
FROM SOURCE_TABLE_1
UNION
SELECT 1
,2
,'LOADING'
,'SAVING'
,COL_DCONFIRMDATE
,COL_DATTACHMENTDATE
FROM SOURCE_TABLE_2;
就我而言,错误出现在带有 SELECT
的 UNION
语句中;但是,当我进一步检查错误的根源时,错误确实是由 SELECT
语句中使用的列的数据类型之间的不匹配造成的,而不是在 SQL Developer 中显示的红色突出显示的行中,而是在下一个SELECT
语句中。
我进行了更改并成功编译了存储过程。
示例:
INSERT INTO TABLE_TO_INSERT (
COL_FIELD_1
,COL_FIELD_2
,COL_FIELD_3
,COL_FIELD_4
,COL_FIELD_5
,COL_FIELD_6
)
SELECT 1
,2
,'IMPORT'
,'EXPORT'
,COL_DEXPIRATION
,COL_DEXPIRATION
FROM SOURCE_TABLE_1
UNION
SELECT 1
,2
,'LOADING'
,'SAVING'
,CAST(COL_DCONFIRMDATE AS VARCHAR2 (500)) -- > The real ORA-01790 was at this line and this was the change made.
,CAST(COL_DATTACHMENTDATE VARCHAR2 (500)) -- > The real ORA-01790 was at this line and this was the change made.
FROM SOURCE_TABLE_2;
TL;DR:Oracle 很奇怪,请检查突出显示行之外的错误,并尽可能保持您的代码整洁有序。