Solved

oracle  how to drop multiple tables in a script

Posted on 2015-01-04
5
1,075 Views
Last Modified: 2015-01-19
getting error in this script
--*********************************************
--*  R311013 RESTORE                          *
--*  Restore table from backup tables.        *
--*  Delete current table                     *
--*  Create table by copying from bkup table  *
--*********************************************
DECLARE
   table_does_not_exist   EXCEPTION;
   table_does_not_exist2  EXCEPTION;
   PRAGMA EXCEPTION_INIT (table_does_not_exist, -00942);    -- ORA-00942
   table_1                VARCHAR2 (30) := 'SIMDATA.TEMP_R311013_ACCEL';
   table_2                VARCHAR2 (30) := 'SIMDATA.TEMP_R311013_CLASSIC';
   
BEGIN
   EXECUTE IMMEDIATE('DROP TABLE ' || table_1);
   EXCEPTION WHEN table_does_not_exist THEN NULL;  
 
   EXECUTE IMMEDIATE
         'create table '
      || table_1
      || ' AS SELECT * FROM '
      || table_1    
      || '_BKUP';

  EXECUTE IMMEDIATE('DROP TABLE ' || table_2);
  EXCEPTION WHEN table_does_not_exist2 THEN NULL;  

    EXECUTE IMMEDIATE
          'create table '
       || table_2
       || ' AS SELECT * FROM '
       || table_2
       || '_BKUP';

END;
/
R311013-Restore-Tables.sql
0
Comment
Question by:ALad2005
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 20

Accepted Solution

by:
flow01 earned 250 total points
ID: 40530586
what error do you get ?
PLS-00103: Encountered the symbol "EXCEPTION" when expecting one of the ?

add an extra begin end to make the exeption at the end of block

DECLARE
   table_does_not_exist   EXCEPTION;
   PRAGMA EXCEPTION_INIT (table_does_not_exist, -00942);    -- ORA-00942
   table_1                VARCHAR2 (30) := 'SIMDATA.TEMP_R311013_ACCEL';
   table_2                VARCHAR2 (30) := 'SIMDATA.TEMP_R311013_CLASSIC';
   
BEGIN
   BEGIN
     EXECUTE IMMEDIATE('DROP TABLE ' || table_1);
   EXCEPTION WHEN table_does_not_exist THEN NULL;  
   END;
   
   EXECUTE IMMEDIATE
         'create table '
      || table_1
      || ' AS SELECT * FROM '
      || table_1    
      || '_BKUP';

  BEGIN
    EXECUTE IMMEDIATE('DROP TABLE ' || table_2);
  EXCEPTION WHEN table_does_not_exist THEN NULL;  
  END;

    EXECUTE IMMEDIATE
          'create table '
       || table_2
       || ' AS SELECT * FROM '
       || table_2
       || '_BKUP';

END;
/
0
 
LVL 28

Assisted Solution

by:Naveen Kumar
Naveen Kumar earned 250 total points
ID: 40530768
Hi,

try the below. nothing much of functional changes but have added the exception sections.

for any exception handling block / dynamic sql while you are working/testing on the script, it is always good to have debug/error messages written to the screen or to the table etc in order for you to clearly understand the code flow/execution. May be you comment out some of the debug/error messages if you do not want once your script if fully tested and ready.

DECLARE
    code_step_no number := 0;

    table_does_not_exist   EXCEPTION;
    PRAGMA EXCEPTION_INIT (table_does_not_exist, -00942);    -- ORA-00942
    table_1                VARCHAR2 (30) := 'SIMDATA.TEMP_R311013_ACCEL';
    table_2                VARCHAR2 (30) := 'SIMDATA.TEMP_R311013_CLASSIC';

   
   
 BEGIN
  code_step_no := 10;

    BEGIN
      EXECUTE IMMEDIATE 'DROP TABLE ' || table_1 ;
    EXCEPTION
WHEN table_does_not_exist THEN
   dbms_output.put_line('in exception section 1: after drop table ' || table_1 || '. code step no:' || code_step_no);
   dbms_output.put_line('sqlcode:' || sqlcode );
   dbms_output.put_line('sqlerrm : ' || sqlerrm );

    END;
   
  code_step_no:=20;
    EXECUTE IMMEDIATE
          'create table '
       || table_1
       || ' AS SELECT * FROM '
       || table_1    
       || '_BKUP';

code_step_no:=30;
   BEGIN
     EXECUTE IMMEDIATE('DROP TABLE ' || table_2);
   EXCEPTION
WHEN table_does_not_exist THEN
   dbms_output.put_line('in exception section 1: after drop table ' || table_2 || '. code step no:' || code_step_no);
   dbms_output.put_line('sqlcode:' || sqlcode );
   dbms_output.put_line('sqlerrm : ' || sqlerrm );
   END;

code_step_no:=40;
     EXECUTE IMMEDIATE
           'create table '
        || table_2
        || ' AS SELECT * FROM '
        || table_2
        || '_BKUP';

exception
WHEN others THEN
   dbms_output.put_line('in others exception section. code step no:' || code_step_no);
   dbms_output.put_line('sqlcode:' || sqlcode );
   dbms_output.put_line('sqlerrm : ' || sqlerrm );

 END;
 /
0
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 40530770
please test and post your error message otherwise we could not understand which error you are getting.
0
 

Author Comment

by:ALad2005
ID: 40558271
I don't know how to close the question.  I already accepted the answers given by members.
0
 

Author Closing Comment

by:ALad2005
ID: 40558275
thank you.
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious sideā€¦
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question