0% found this document useful (0 votes)
67 views5 pages

Begin FOR IN Select From Where IN: 'Table' 'VIEW' 'Package' 'Procedure' 'Function' 'Sequence'

The document provides SQL scripts to drop database objects including tables, views, packages, procedures, functions, and sequences. It loops through the user_objects table to dynamically build DROP statements and execute them immediately to remove the objects. It also shows how to generate the DROP statements into an output file for later execution.

Uploaded by

JigneshBS
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views5 pages

Begin FOR IN Select From Where IN: 'Table' 'VIEW' 'Package' 'Procedure' 'Function' 'Sequence'

The document provides SQL scripts to drop database objects including tables, views, packages, procedures, functions, and sequences. It loops through the user_objects table to dynamically build DROP statements and execute them immediately to remove the objects. It also shows how to generate the DROP statements into an output file for later execution.

Uploaded by

JigneshBS
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

BEGIN FOR cur_rec IN (SELECT object_name, object_type FROM user_objects WHERE object_type IN ('TABLE', 'VIEW', 'PACKAGE', 'PROCEDURE', 'FUNCTION',

'SEQUENCE' )) LOOP BEGIN IF cur_rec.object_type = 'TABLE' THEN EXECUTE IMMEDIATE 'DROP ' || cur_rec.object_type || ' "' || cur_rec.object_name || '" CASCADE CONSTRAINTS'; ELSE EXECUTE IMMEDIATE 'DROP ' || cur_rec.object_type || ' "' || cur_rec.object_name || '"'; END IF; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.put_line ( 'FAILED: DROP ' || cur_rec.object_type || ' "' || cur_rec.object_name || '"' ); END; END LOOP; END; /

SET SET SET SET

NEWPAGE 0 SPACE 0 LINESIZE 80 PAGESIZE 0

SET ECHO OFF SET FEEDBACK OFF SET HEADING OFF SET MARKUP HTML OFF SET ESCAPE \ SPOOL drop_tables.sql select 'drop table ', table_name, 'cascade constraints \;' from user_tables; / select 'drop sequence ',object_name,'\;' from user_objects where object_type='SEQUENCE'; / SPOOL OFF @drop_tables.sql SET ECHO ON SET FEEDBACK ON SET HEADING ON

set heading off; set feedback off; spool C:my_file_name.sql select 'delete from ' || tname ||';' from tab where tabtype = 'TABLE'; spool off;

set heading off; set feedback off; spool C:my_file_name.sql select 'drop table ' || table_name || 'cascade constraints;' from user_tables; spool off;

SELECT COUNT(*) "ACTIVE USERS" FROM v_$session WHERE username IS NOT NULL;

select lpad(' ', 2*level) || granted_role "User, his roles and privileges" from ( /* THE USERS */ select null grantee, username granted_role from dba_users where username like upper('%&enter_username%') /* THE ROLES TO ROLES RELATIONS */ union select grantee, granted_role from dba_role_privs /* THE ROLES TO PRIVILEGE RELATIONS */ union select grantee, privilege from dba_sys_privs ) start with grantee is null connect by grantee = prior granted_role;

-SELECT username, password, created, password_versions FROM dba_users ORDER BY 1;

For all tables ============== SELECT 'DROP TABLE ' || owner || '.' || table_name || ' CASCADE CONSTRAINTS;' FROM all_tables /

For all triggers ================ SELECT 'DROP TRIGGER ' || owner || '.' || trigger_name || ' ;' FROM all_triggers /

For all sequences ================= SELECT 'DROP SEQUENCE ' || sequence_owner || '.' || sequence_name || ' ;' FROM all_sequences / Custom for specific owners/users (but make sure you log in as SYSDBA) Also substitute the sql above if tables, triggers or sequences and include/exclude users/owners as needed ====================================================================

Example for sequence SELECT 'DROP SEQUENCE ' || sequence_owner || '.' || sequence_name || ';' FROM all_sequences where sequence_owner IN ( 'SCOTT', 'SH', 'SSS', 'TEST1', . . . . . 'WHD' )

-------Here is the SQL Script: Begin for c in (select table_name from user_tables) loop execute immediate ('drop table '||c.table_name||' cascade constraints); end loop; End; Take note!! 1. You need to log in to that user which you wanted to drop the tables 1. DECLARE 2. 3. 4. 5. 6. table_name VARCHAR2(30); CURSOR usertables IS SELECT * FROM user_tables WHERE table_name NOT LIKE 'BIN$%'; BEGIN FOR next_row IN usertables LOOP

7. 8. 9.

EXECUTE IMMEDIATE 'drop table ' || next_row.table_name || ' cascade constraints'; END LOOP; END;

save
drop.sql: ================================ set heading off set newpage none set serveroutput on size 100000 DECLARE CUR INTEGER; OBJNAME VARCHAR2(128); CURSOR C1 IS SELECT OBJECT_NAME FROM USER_OBJECTS WHERE OBJECT_TYPE='TABLE'; BEGIN OPEN C1; LOOP BEGIN FETCH C1 INTO OBJNAME; EXIT WHEN C1%NOTFOUND; CUR:=DBMS_SQL.OPEN_CURSOR; DBMS_OUTPUT.PUT_LINE('Dropping' || ' TABLE ' || OBJNAME); DBMS_SQL.PARSE(CUR, 'DROP TABLE ' || OBJNAME || ' CASCADE CONSTRAINTS' , DBMS_SQL.NATIVE); DBMS_SQL.CLOSE_CURSOR(CUR); EXCEPTION WHEN OTHERS THEN dbms_output.put_line('Exception encountered for ' || OBJNAME); END; END LOOP; CLOSE C1; COMMIT; EXCEPTION WHEN OTHERS THEN NULL; END; /

You might also like