0

I was wondering if there is someway i can handle a exception if i get an error while creating a table during a procedure.

IF testes=0 then                                                                                
stmt:= 'create table ' || prefix || SII_BCK_TAB_ID_SEQ.nextval || ' AS SELECT * FROM '|| n_tab || ' WHERE 1=0';
EXECUTE IMMEDIATE stmt;

can i create a exception after executing the statement? what is the best process to handle errors while creating a table? or is it the same as handling dml statements?

can i insert something like savepoints? thank you

3
  • 1
    You handle exceptions to execute immediate. It doesn't matter what the underlying statement is. The exception handling code would deal with that in PL/SQL. Commented Nov 9, 2018 at 15:37
  • ok thank you for the clarification Commented Nov 9, 2018 at 15:39
  • @GordonLinoff can i still use savepoint normally if the statement fails? Commented Nov 9, 2018 at 17:06

1 Answer 1

1

If I were you, I'd create a separate procedure to handle the table creation and then have an exception clause. This makes it modular code, then; easy to unit test, etc.

e.g.: the procedure would look something like:

PROCEDURE create_table (in_new_table_name in varchar2,
                        in_old_table_name in varchar2)
is
  E_TAB_EXISTS EXCEPTION;
  PRAGMA EXCEPTION_INIT(E_TAB_EXISTS,-955);
BEGIN
  execute immediate 'create table ' || in_new_table_name || ' AS SELECT * FROM '|| in_old_table_name || ' WHERE 1=0';
EXCEPTION
  WHEN E_TAB_EXISTS THEN
    NULL;
END create_table;

and you would call it like:

If testes = 0 then
  create_table(in_new_table_name => prefix || sii_bck_tab_id_seq.nextval,
               in_old_table_name => n_tab);
  ...
end if;

Ideally, you're creating your code in a package, and can simply have the new procedure as a separate procedure in the package.

Sign up to request clarification or add additional context in comments.

1 Comment

thats what im doing, its part of a job im creating, so i could create a new procedure for my create table. thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.