无法使用合并语句合并 Oracle 数据库中的记录

Not able to merge records in Oracle database using merge statement

SELECT * FROM DIM_TRANS_TYPE WHERE TRANSACTION_TYPE='ILAU';

我想在这里完成的是在 above-mentioned 记录中将 ILAU 更改为 IFAU 并将 Instant Loan Authorization Request 更改为 Instant Finance Authorization Request

Table 架构:

Name                  Null     Type               
--------------------- -------- ------------------ 
TRANSACTION_TYPE      NOT NULL VARCHAR2(4 CHAR)   --> PRIMARY KEY
TRANSACTION_TYPE_DESC          VARCHAR2(256 CHAR) 

成功了!!!

MERGE INTO DIM_TRANS_TYPE a
USING (SELECT 'ILAU' TRANSACTION_TYPE, 'Instant Loan Authorization Request' TRANSACTION_TYPE_DESC FROM DUAL) b
    ON (a.TRANSACTION_TYPE = b.TRANSACTION_TYPE)
WHEN MATCHED THEN UPDATE
    SET
      a.TRANSACTION_TYPE_DESC = 'Instant Finance Authorization Request'
WHEN NOT MATCHED THEN
    INSERT(a.TRANSACTION_TYPE, a.TRANSACTION_TYPE_DESC)
    VALUES(b.TRANSACTION_TYPE, b.TRANSACTION_TYPE_DESC);

不起作用!!! (错误如下图)

MERGE INTO DIM_TRANS_TYPE a
USING (SELECT 'ILAU' TRANSACTION_TYPE, 'Instant Loan Authorization Request' TRANSACTION_TYPE_DESC FROM DUAL) b
    ON (a.TRANSACTION_TYPE = b.TRANSACTION_TYPE)
WHEN MATCHED THEN UPDATE
    SET
      a.TRANSACTION_TYPE = 'IFAU',
      a.TRANSACTION_TYPE_DESC = 'Instant Finance Authorization Request'
WHEN NOT MATCHED THEN
    INSERT(a.TRANSACTION_TYPE, a.TRANSACTION_TYPE_DESC)
    VALUES(b.TRANSACTION_TYPE, b.TRANSACTION_TYPE_DESC);

错误:

Error at Command Line : 5 Column : 9
Error report -
SQL Error: ORA-38104: Columns referenced in the ON Clause cannot be updated: "A"."TRANSACTION_TYPE"
38104. 00000 -  "Columns referenced in the ON Clause cannot be updated: %s"
*Cause:    LHS of UPDATE SET contains the columns referenced in the ON Clause
*Action:

如错误消息所示,无法更新 on 子句中使用的字段。

您必须在单独的语句中进行此类更新。像这样:

UPDATE DIM_TRANS_TYPE
SET    TRANSACTION_TYPE = 'IFAU',
       TRANSACTION_TYPE_DESC = 'Instant Finance Authorization Request'
WHERE  TRANSACTION_TYPE IN (
            SELECT TRANSATION_TYPE
            FROM   (SELECT 'ILAU' TRANSACTION_TYPE, 
                           'Instant Loan Authorization Request' TRANSACTION_TYPE_DESC 
                    FROM DUAL)
       );

MERGE INTO DIM_TRANS_TYPE a
USING      (SELECT 'ILAU' TRANSACTION_TYPE, 
                   'Instant Loan Authorization Request' TRANSACTION_TYPE_DESC 
            FROM   DUAL) b
        ON (a.TRANSACTION_TYPE = b.TRANSACTION_TYPE)
WHEN NOT MATCHED THEN
    INSERT(TRANSACTION_TYPE, TRANSACTION_TYPE_DESC)
    VALUES(b.TRANSACTION_TYPE, b.TRANSACTION_TYPE_DESC);

您可能希望在事务中执行此操作,以便提交由这两个语句或都不提交的更改。

当然,IN 子句看起来有点夸张,因为该值可以简单地与 ILAU 进行比较,但我假设您简化了问题,而实际上您有更大的数据集要获取transaction_type 值来自.

也许我遗漏了一些东西....您似乎正在寻找 transaction_type = 'ILAU'regardless of transaction_type_desc)的所有行,并将这些行更新为 transaction_type'IFAU'transaction_type_desc'Instant Finance Authorization Request'。对吗?

如果是这样,为什么除了琐碎的 UPDATE 声明之外还需要其他任何东西?

update  dim_trans_type
  set   transaction_type      = 'IFAU',
        transaction_type_desc = 'Instant Finance Authorization Request'
  where transaction_type      = 'ILAU'
;

在您的尝试中,还有一个 when not matched 子句 - 如果 none 现有行具有 transaction_type = 'ILAU',则插入一行。这与您的要求有何关系?你说 我想在这里完成的只是改变 [.....] - 没有提到插入任何东西。问题陈述,用简单的英语,不完整吗?还是你在插入不需要插入的东西?