Monday, July 5, 2021

PL/SQL procedure to Drop all the objects from the oracle user

Procedure to drop the objects from a Oracle Schema : 

Below is the pl/sql procedure to drop the user objects using condition. 
Use it with EXTREME CAUTION, as the below steps of procedure drops the objects from the connected user. 


STEP 1: 
Create a temporary table with object_name and object_type as the columns, create a combined index as it gives better performance. 

SQL 1: create table DEV1_OBJECTS_DEL as select object_name, object_type from user_objects; 
SQL 2: create unique index ix_DEV_OBJECTS on DEV1_OBJECTS_DEL( object_name, object_type ) parallel 10; 


STEP 2: 
Run the below procedure, the procedure refers to the 'DEV1_OBJECTS_DEL' table and drops the objects in the table. 


PL/SQL 1: 
----------------------------------------------------------------------------------------- 
SET SERVEROUTPUT ON;
DECLARE
cursor cur is SELECT object_name, object_type FROM  DEV1_OBJECTS_DEL;
BEGIN
FOR cur_rec IN CUR LOOP
BEGIN
EXECUTE IMMEDIATE 'DROP ' || cur_rec.object_type || ' "' || cur_rec.object_name || '"';
EXCEPTION
WHEN OTHERS THEN
NULL;
END;
END LOOP;
END;
/
---------------------------------------------------------------------------------------------- 

NOTE: SYS objects and SYSTEM generated constraints cannot be dropped using the procedure, you need to drop it manually. 

Happy Coding.

No comments:

Post a Comment