drop tablespace if do not exist

陌路散爱 提交于 2020-01-24 08:58:06

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/6180710/drop-tablespace-if-do-not-exist

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!