Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

drop tablespace if do not exist

I have written pl/sql script (works, but doesn't look nice):

DECLARE
   v_exists NUMBER;
BEGIN
   SELECT count(*) INTO v_exists FROM dba_tablespaces WHERE tablespace_name = 'hr_test';
   IF v_exists > 0 THEN
   BEGIN
      EXECUTE IMMEDIATE 'DROP TABLESPACE hr_test INCLUDING CONTENTS AND DATAFILES CASCADE CONSTRAINTS';
   END;
   END IF;
   EXECUTE IMMEDIATE 'CREATE TABLESPACE hr_RJ DATAFILE ''E:\hr_test_01.dbf'' SIZE 16M';
END;

Is there any way to rewrite this script without EXECUTE IMMEDIATE?

like image 863
spiaire Avatar asked May 30 '11 21:05

spiaire


People also ask

How would you drop a tablespace if the tablespace were not empty?

You must first remove the tablespace from the database default temporary tablespace group and then drop it. You cannot drop a tablespace, even with the INCLUDING CONTENTS and CASCADE CONSTRAINTS clauses, if doing so would disable a primary key or unique constraint in another tablespace.

How do I drop a tablespace in Oracle 19c?

Introduction to Oracle DROP TABLESPACE statement First, specify the name of the tablespace that you want to drop after the DROP TABLESPACE keywords. Second, use the INCLUDE CONTENTS to delete all contents of the tablespace. If the tablespace has any objects, you must use this option to remove the tablespace.

Can we drop Sysaux tablespace?

You cannot drop the SYSTEM tablespace. You can drop the SYSAUX tablespace only if you have the SYSDBA system privilege and you have started the database in MIGRATE mode. You may want to alert any users who have been assigned the tablespace as either a default or temporary tablespace.


1 Answers

No. You cannot issue DDL statements in static PL/SQL.

And yes, it is perfectly fine to use native dynamic SQL for DDL purposes:

You need dynamic SQL in the following situations:

You want to execute a SQL data definition statement (such as CREATE), a data control statement (such as GRANT), or a session control statement (such as ALTER SESSION). In PL/SQL, such statements cannot be executed statically.

Oracle dynamic SQL

like image 189
Kirill Leontev Avatar answered Oct 19 '22 20:10

Kirill Leontev