我正在尝试通过Merge语句在oracle中执行Upsert。 现在,我正在尝试合并的表有一个组合的主要, 所以我像这样创建了我的查询:
MERGE INTO CONTINGENT_COMMISSION CC USING
(SELECT
'ad_adavila' CC_PROD_USERID,
to_number('42.22', '999,999,999.99') CC_COMM_PERCENT,
to_number('217,904.08', '999,999,999.99') CC_PREMIUM,
'2' CC_TRIMESTER,
'2013' CC_YEAR
FROM DUAL) SRC
ON (CC.CC_PROD_USERID || CC.CC_TRIMESTER || CC.CC_YEAR = SRC.CC_PROD_USERID || SRC.CC_TRIMESTER || SRC.CC_YEAR)
WHEN MATCHED THEN
UPDATE
SET CC.CC_COMM_PERCENT = to_number('42.22', '999,999,999.99'),
CC.CC_PREMIUM = to_number('217,904.08', '999,999,999.99'),
CC.CC_TRIMESTER = '2',
CC.CC_YEAR = '2013'
WHEN NOT MATCHED THEN
INSERT
(CC_PROD_USERID,CC_COMM_PERCENT,CC_PREMIUM,CC_TRIMESTER,CC_YEAR)
VALUES
('ad_adavila',to_number('42.22', '999,999,999.99'),to_number('217,904.08', '999,999,999.99'),'2','2013');
现在,每当我尝试运行此查询时,我都会收到以下错误。
ORA-38104:无法更新ON子句中引用的列:“CC”。“CC_TRIMESTER”
通常这是因为ON子句有更多的一列;因为组合的主键,这在我的情况下是必须的:
CONSTRAINT "CONTINGENT_COMMISSION_PK" PRIMARY KEY ("CC_PROD_USERID", "CC_TRIMESTER", "CC_YEAR")
有没有让这项工作?
谢谢,
答案 0 :(得分:8)
ORA-38104:ON子句中引用的列无法更新:" CC"。" CC_TRIMESTER"
您无法在on子句中更新用于过滤的列,只需从更新部分中删除此列
merge into CONTINGENT_COMMISSION CC
using (select 'ad_adavila' CC_PROD_USERID,
to_number('42.22', '999,999,999.99') CC_COMM_PERCENT,
to_number('217,904.08', '999,999,999.99') CC_PREMIUM,
'2' CC_TRIMESTER,
'2013' CC_YEAR
from DUAL) SRC
on (CC.CC_PROD_USERID || CC.CC_TRIMESTER || CC.CC_YEAR = SRC.CC_PROD_USERID || SRC.CC_TRIMESTER || SRC.CC_YEAR)
when matched then
update
set CC.CC_COMM_PERCENT = to_number('42.22', '999,999,999.99'),
CC.CC_PREMIUM = to_number('217,904.08', '999,999,999.99')
when not matched then
insert
(CC_PROD_USERID, CC_COMM_PERCENT, CC_PREMIUM, CC_TRIMESTER, CC_YEAR)
values
('ad_adavila', to_number('42.22', '999,999,999.99'), to_number('217,904.08', '999,999,999.99'), '2', '2013');