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
?
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With