How to drop table in Oracle procedure?

How to  drop table in Oracle procedure?

CREATE OR REPLACE PROCEDURE deletetable AS
BEGIN
  DROP TABLE step1;
END;
/

Warning: Procedure created with compilation errors.

SQL> SHOW ERRORS
Errors for PROCEDURE DELETETABLE:

LINE/COL ERROR
-------- -----------------------------------------------------------------
3/3      PLS-00103: Encountered the symbol "DROP" when expecting one of
         the following:....
apergielAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bkesarlaCommented:
Hi,

  Use DBMS_SQL Package to execute  DDL Stmts from PL/SQL.

Try the foll :

CREATE OR REPLACE PROCEDURE deletetable AS
  cursor1 INTEGER;
  BEGIN
    cursor1 := dbms_sql.open_cursor;
    dbms_sql.parse(cursor1, 'DROP TABLE <tableName>' ,   dbms_sql.v7);
    dbms_sql.close_cursor(cursor1);
  end;
 /


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tbcoxCommented:
In version 8.1.x you can issue this command in PL/SQL:

EXECUTE IMMEDIATE 'drop table step1';

For prior versions, it's trickier -- DDL is not directly supported in PL/SQL the way DML is.  You would call DBMS_SQL.open_cursor, then DBMS_SQL.parse, then DBMS_SQL.execute.

As Oracle says in the package header of DBMS_SQL:  

  --  NOTE THAT PARSING AND
  --  EXECUTING DDL STATEMENTS CAN CAUSE HANGS!
  --  Currently, the deferred parsing feature of the Oracle  Call Interface
  --  is not used. As a result, statements are parsed immediately. In addition,
  --  DDL statements are executed immediately when parsed. However,
  --  the behavior may
  --  change in the future so that the actual parsing (and execution of DDL
  --  statement) do not occur until the cursor is executed with "execute".
  --  DO NOT RELY ON THE CURRENT TIMING OF THE ACTUAL PARSING!

Those are your only choices, so far as I am aware, for performing DDL in PL/SQL blocks.

 -Tom

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.