I'm running the PL/SQL block below from a script. As you can see, it adds a column, and should catch any exceptions - among them, the one that would get thrown in the column already existed.
Now if I run this and the column already exists, I get an error:
Error code: -1735
Error message: ORA-01735: invalid ALTER TABLE option
That is all well and good, but if I run the inner SQL instead though; that is the SQL that follows execute immediate on it's own, I get this message instead, which is more precise:
ORA-01430: column being added already exists in table
The first error has the error code -1735, and I am able to catch with the when-clause that is commented out in the code below; if it is not commented out, the result will instead be:
Some other error occurred
I am not able to catch the -1430 exception though, even though that seems to be the root cause of the exception.
So my question: Is there any way to access this "inner" exception in this case? (is that even a valid term in this case?) In other words, can this be modified so as to provide a more specific error message?
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;
/
;within'..... VARCHAR2(10 CHAR));'and try again.SELECT * FROM USER_TAB_COLS WHERE table_name = 'MY_TABLE';to see whether column has been added already.