Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

oracle  how to drop multiple tables in a script

Posted on 2015-01-04
5
Medium Priority
?
1,394 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
  • 2
  • 2
5 Comments
 
LVL 21

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 Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

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…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

972 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