PL/SQL MERGE INTO 编译错误
Posted
技术标签:
【中文标题】PL/SQL MERGE INTO 编译错误【英文标题】:PL/SQL MERGE INTO Compilation Errors 【发布时间】:2014-05-30 13:30:51 【问题描述】:我想使用 PL/SQL MERGE 对定义为
的单个表执行 UPSERTDECLARE
v_create LONG;
v_drop LONG;
TABLE_DOES_NOT_EXIST exception;
PRAGMA EXCEPTION_INIT(TABLE_DOES_NOT_EXIST, -942); -- ORA-00942
TABLE_NAME VARCHAR2(30) := 'B2BOWNER.SSC_Page_Map';
BEGIN
BEGIN
v_drop := 'DROP TABLE ' || TABLE_NAME;
EXECUTE IMMEDIATE v_drop;
EXCEPTION
WHEN TABLE_DOES_NOT_EXIST THEN NULL;
END;
v_create := 'CREATE TABLE ' || TABLE_NAME || ' (
PAGE_ID_NBR NUMERIC(10) NOT NULL Check(Page_ID_NBR > 0),
PAGE_TYPE VARCHAR2(50) NOT NULL,
PAGE_DCPN VARCHAR2(100) NOT NULL,
PRIMARY KEY(Page_ID_NBR, Page_Type))';
EXECUTE IMMEDIATE v_create;
COMMIT WORK;
COMMIT COMMENT 'Create Table';
EXCEPTION
WHEN OTHERS THEN
IF SQLCODE = -955 THEN
NULL; -- suppresses ORA-00955 exception
ELSE
RAISE;
END IF;
END;
在 PL/SQL MERGE 中使用以下尝试,如下所示
CREATE OR REPLACE FUNCTION B2BOWNER.F_SSC_Page_Map_Update(
p_page_id IN B2BOWNER.SSC_Page_Map.Page_ID_NBR%TYPE,
p_page_type IN B2BOWNER.SSC_Page_Map.Page_Type%TYPE,
p_page_dcpn IN B2BOWNER.SSC_Page_Map.Page_Dcpn%TYPE)
RETURN INTEGER
IS
TABLE_DOES_NOT_EXIST exception;
PRAGMA EXCEPTION_INIT(TABLE_DOES_NOT_EXIST, -942); -- ORA-00942
BEGIN
MERGE INTO SSC_Page_Map d
USING (SELECT PAGE_ID_NBR, PAGE_TYPE from dual)
ON(d.PAGE_ID_NBR = p_page_id AND d.PAGE_TYPE = p_page_type)
WHEN MATCHED THEN UPDATE SET d.PAGE_ID_NBR= p_page_id, d.PAGE_TYPE=p_page_type
WHEN NOT MATCHED THEN INSERT (PAGE_ID_NBR, PAGE_TYPE, PAGE_DCPN) VALUES (p_page_id, p_page_type, p_page_dcpn);
COMMIT WORK;
COMMIT COMMENT 'Modifying Type and Description Only';
RETURN 0;
EXCEPTION
WHEN TABLE_DOES_NOT_EXIST THEN
RETURN -1;
WHEN DUP_VAL_ON_INDEX THEN
RETURN -2;
WHEN INVALID_NUMBER THEN
RETURN -3;
WHEN OTHERS THEN
RETURN -4;
END F_SSC_Page_Map_Update;
SHOW ERRORS PROCEDURE B2BOWNER.F_SSC_Page_Map_Update;
GRANT EXECUTE ON B2BOWNER.F_SSC_Page_Map_Update TO B2B_USER_DBROLE
我得到了一个
Warning: compiled but with compilation errors
No errors.
Grant complete.
详细说明
[Warning] ORA-24344: success with compilation error
16/31 PL/SQL: ORA-00904: "PAGE_TYPE": invalid identifier
15/4 PL/SQL: SQL Statement ignored
(2: 0): Warning: compiled but with compilation errors
如何编写 MERGE 以便编译 PL/SQL 函数而不会出错?
【问题讨论】:
不应该是THEN INSERT (d.PAGE_ID_NBR, d.PAGE_TYPE, d.PAGE_DCPN) ...
吗?
【参考方案1】:
在我看来,MERGE 语句应该是:
MERGE INTO SSC_PAGE_MAP d
USING (SELECT p_page_id AS PAGE_ID_NBR, p_page_type AS PAGE_TYPE from dual)
ON(d.PAGE_ID_NBR = p_page_id AND
d.PAGE_TYPE = p_page_type)
WHEN MATCHED THEN
UPDATE SET d.PAGE_DCPN = p_page_dcpn
WHEN NOT MATCHED THEN
INSERT
(PAGE_ID_NBR, PAGE_TYPE, PAGE_DCPN)
VALUES
(p_page_id, p_page_type, p_page_dcpn);
分享和享受。
【讨论】:
以上是关于PL/SQL MERGE INTO 编译错误的主要内容,如果未能解决你的问题,请参考以下文章
Oracle使用merge into 编写存储过程 遇编译错误:PL/SQL: ORA-00926: 缺失 VALUES 关键字
Oracle使用merge into 编写存储过程 遇编译错误:PL/SQL: ORA-00926: 缺失 VALUES 关键字
ORA-00904 在 DATE 列上使用“MERGE INTO”和“SELECT FROM dual”时出现“无效标识符”错误