1

下面我的 UPDATE_OR_INSERT 代码有什么问题?

MERGE INTO EMAIL_LIST d USING (SELECT 'foo@gmail.com' EMAIL) s
    ON (d.EMAIL = s.EMAIL)
        WHEN MATCHED THEN
            UPDATE SET d.EMAIL = s.EMAIL
        WHEN NOT MATCHED THEN
            INSERT (EMAIL) VALUES (s.EMAIL);

给定表格:

CREATE TABLE EMAIL_LIST ( 
    EMAIL VARCHAR2 (100)  NOT NULL
);
ALTER TABLE EMAIL_LIST
    ADD CONSTRAINT PK_EMAIL_LIST PRIMARY KEY ( EMAIL ) ;

错误

错误报告:

SQL 错误:ORA-00923:FROM 关键字未在预期的位置找到 00923。00000 -“FROM 关键字未在预期的位置找到”

4

1 回答 1

4

几件事;

您需要从某些东西中选择常数,在 Oracle 的情况下,DUAL;

MERGE INTO EMAIL_LIST d 
USING 
    (SELECT 'foo@gmail.com' EMAIL FROM DUAL) s
ON (d.EMAIL = s.EMAIL)
WHEN MATCHED THEN
    UPDATE SET d.EMAIL = s.EMAIL
WHEN NOT MATCHED THEN
    INSERT (EMAIL) VALUES (s.EMAIL);

此外,您无法在 d.EMAIL 上匹配并同时更新它。您的线路;

WHEN MATCHED THEN
    UPDATE SET d.EMAIL = s.EMAIL

无论如何都没有意义,因为 d.EMAIL 已经等于 s.EMAIL 或者匹配不会发生。删除WHEN MATCHED,您将完成工作;

MERGE INTO EMAIL_LIST d 
USING 
    (SELECT 'foo@gmail.com' EMAIL FROM DUAL) s
ON (d.EMAIL = s.EMAIL)
WHEN NOT MATCHED THEN
    INSERT (EMAIL) VALUES (s.EMAIL);
于 2012-09-26T08:31:25.377 回答