Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

oracle  how to drop multiple tables in a script

Posted on 2015-01-04
5
Medium Priority
?
1,290 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 1000 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 1000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

721 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