创建PL / SQL FUNCTION时收到以下内容
Warning: compiled but with compilation errors
No errors.
当我在TOAD中查看消息时,我看到以下
[Warning] ORA-24344: success with compilation error
17/43 PL/SQL: ORA-00904: "PAGE_DCPN": invalid identifier
2/48 PLS-00201: identifier 'B2BOWNER.SSC_PAGE_MAP' must be declared
16/5 PL/SQL: SQL Statement ignored
PL/SQL: Compilation unit analysis terminated
(2: 0): Warning: compiled but with compilation errors
我正在使用以下PL / SQL命令来创建表
DECLARE
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函数是
CREATE OR REPLACE FUNCTION B2BOWNER.F_SSC_Page_Map_Insert(
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 B2BOWNER.SSC_PAGE_MAP
USING (SELECT PAGE_ID_NBR ,PAGE_TYPE ,PAGE_DCPN FROM dual)
ON (PAGE_ID_NBR = p_page_id AND PAGE_TYPE = p_page_type AND PAGE_DCPN = p_page_dcpn)
WHEN MATCHED THEN
UPDATE SET PAGE_ID_NBR = p_page_id, PAGE_TYPE = p_page_type, PAGE_DCPN = p_page_dcpn
WHEN NOT MATCHED THEN
INSERT (PAGR_ID_NBR, PAGE_TYPE, PAGE_DCPN)
VALUES(p_page_id, p_page_type, p_page_dcpn);
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_Insert;
SHOW ERRORS PROCEDURE B2BOWNER.F_SSC_Page_Map_Insert;
GRANT EXECUTE ON F_SSC_Page_Map_Insert TO B2B_USER_DBROLE;
PL / SQL创建脚本使用通常的
执行成功 PL/SQL procedure successfully completed.
并在模式B2BOWNER.SSC_PAGE_MAP
下的toad中验证为现有,并且看起来都很好。
我很擅长使用MERGE
程序
here所以这可能是我的问题所在,尽管程序的哪个部分并不明显。
答案 0 :(得分:1)
您无法在F_SSC_Page_Map_Insert
函数中捕获ORA-00942错误,至少不会写入错误。如果在创建函数时表不存在,则该函数将无法编译,因为在创建函数时对表的静态引用不存在。您无法捕获PL / SQL块中的编译错误,只能捕获运行时错误。
我想,您可以修改该函数以仅使用动态SQL来引用该表。这将导致编译器延迟解析对运行时的对象引用,而不是在编译时,这将允许您捕获函数中的异常。当表格不存在时,这也允许函数成功编译。当然,这也会使您的功能更复杂,更难编写,更难阅读,更难调试。它几乎肯定会施加一些性能惩罚。
是否有理由需要在表存在之前创建过程?通常,这是您真正不需要的复杂性。因此,您应该避免复杂性,而不是添加代码来尝试处理它。