【发布时间】:2020-02-11 00:58:21
【问题描述】:
我正在从脚本运行下面的 PL/SQL 块。如您所见,它添加了一个列,并且应该捕获任何异常——其中,将被抛出该列的异常已经存在。
现在如果我运行这个并且列已经存在,我会得到一个错误:
错误代码:-1735
错误消息:ORA-01735:ALTER TABLE 选项无效
这一切都很好,但是如果我改为运行内部 SQL;那是 execute immediate 自己后面的 SQL,我收到了这条消息,更准确:
ORA-01430: 正在添加的列已存在于表中
第一个错误的错误代码为 -1735,我可以使用下面代码中注释掉的 when-clause 来捕获;如果它没有被注释掉,结果将是:
发生了一些其他错误
我无法捕捉到 -1430 异常,尽管这似乎是异常的根本原因。
所以我的问题是:在这种情况下有没有办法访问这个“内部”异常?(在这种情况下这甚至是一个有效的术语吗?)换句话说,可以这样修改吗?为了提供更具体的错误信息?
DECLARE
column_exists exception;
pragma exception_init (column_exists , -1430);
general_error exception;
pragma exception_init (general_error , -1735);
BEGIN
execute immediate 'ALTER TABLE my_table
ADD (some_column VARCHAR2(10 CHAR));';
EXCEPTION
-- I expected / wanted this to catch my error in order
-- to let me output a more specific message:
WHEN column_exists THEN
DBMS_OUTPUT.PUT_LINE('The column or index already exists');
-- Note: Commented out, but would otherwise catch the general error.
-- (I tested it here just to confirm that I can catch exceptions this way)
-- WHEN general_error THEN
-- DBMS_OUTPUT.PUT_LINE('Some other error occurred');
-- General catch: Generates the first message quoted above:
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error code: ' || SQLCODE);
DBMS_OUTPUT.PUT_LINE('Error message: ' || SQLERRM);
END;
/
【问题讨论】:
-
删除
'..... VARCHAR2(10 CHAR));'中的;,然后重试。 -
您也可以查看
SELECT * FROM USER_TAB_COLS WHERE table_name = 'MY_TABLE';查看是否已添加列。
标签: sql oracle exception plsql dynamic-sql