Solved

oracle  how to drop multiple tables in a script

Posted on 2015-01-04
5
1,007 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Import and exporting Oracle Data with encrypted columns 4 68
Fill Null values 5 53
Need to Understand Resolution to Oracle Error ORA-00600 2 52
Oracle Join issue. 3 46
This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

739 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