0

I have a lookup table that contains the owner(schema) and the table_names. So I want to loop through this lookup table to insert the data into a new table from this owner.table. But I want to handle the exception in the loop just in case the owner/table doesn't exist.

But i'm getting this error message when compile the following code. Error(49,21): PLS-00103: Encountered the symbol "EXCEPTION" when expecting one of the following: ( begin case declare end exit for goto if loop mod null pragma raise return select update while with << continue close current delete fetch lock insert open rollback savepoint set sql execute commit forall merge pipe purge json_exists json_value json_query json_object json_array

declare
table_does_not_exist exception;  
PRAGMA EXCEPTION_INIT(table_does_not_exist, -942);

cursor lookup_table is
select owner,table_name
from lookup;

begin
for rec in lookup_table loop
  execute immediate 'insert into new_table select * from '||rec.owner||'.'||rec.table_name;

  exception
  when table_does_not_exist then
      dbms_output.put_line('table dose not exist, continue the interation');
      continue;
end loop;

1 Answer 1

2

Oracle's block structure permits block nesting. So inside your loop you can nest a block which handles your exception.

declare
table_does_not_exist exception;  
PRAGMA EXCEPTION_INIT(table_does_not_exist, -942);

cursor lookup_table is
select owner,table_name
from lookup;

begin
for rec in lookup_table loop 
    begin 
       execute immediate 'insert into new_table select * from '||rec.owner||'.'||rec.table_name;

    exception
        when table_does_not_exist then
          dbms_output.put_line('table dose not exist, continue the interation');
    end ; 
end loop;  
end ;
Sign up to request clarification or add additional context in comments.

Comments

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.