Delete all objects in schema using batch file in Oracle - oracle

I have below Select query which will generate delete statements to delete all objects in schema.
select 'DROP '||OBJECT_TYPE||' '||OWNER||'.'||OBJECT_NAME
|| case when OBJECT_TYPE = 'TABLE'
then ' CASCADE CONSTRAINTS PURGE' else '' end
||';'
from all_objects
where OWNER = 'RATOR_MONITORING';
I want to create batch file and suppose that instead of generating delete statements separetly I can create may be cursor or something and save it in batch file and run the batch file to delete all contents in schema. How to do it?

You can find many script in the Internet. Neither of them work on 100%. There can we various gotchas. Like scheduler chains or materialized view groups.
This is the one I use (it is also inspired by one I found in the Internet)
set serveroutput on size unlimited
declare
v_ItemCount integer;
begin
SELECT count(*)
INTO v_ItemCount
FROM ALL_OBJECTS AO
WHERE AO.OWNER = '&USER'
AND AO.OBJECT_TYPE NOT IN ('INDEX', 'LOB')
AND AO.OBJECT_NAME NOT LIKE 'BIN$%';
while (v_ItemCount > 0) loop
for v_Cmd in (SELECT 'drop ' || AO.OBJECT_TYPE || ' ' || '"'||AO.OWNER||'"'|| '.' || '"'||AO.OBJECT_NAME||'"' ||
DECODE(AO.OBJECT_TYPE,
'TABLE',
' CASCADE CONSTRAINTS',
'') as DROPCMD,
AO.OWNER,
AO.OBJECT_TYPE,
AO.OBJECT_NAME
FROM ALL_OBJECTS AO
WHERE AO.OWNER = '&USER'
AND AO.OBJECT_TYPE NOT IN ('INDEX', 'LOB')
AND AO.OBJECT_NAME NOT LIKE 'BIN$%')
loop
begin
if v_Cmd.OBJECT_TYPE = 'SCHEDULE' then
DBMS_SCHEDULER.DROP_SCHEDULE(v_Cmd.OWNER||'.'||v_Cmd.OBJECT_NAME, true);
ELSIF v_Cmd.OBJECT_TYPE = 'JOB' then
DBMS_SCHEDULER.DROP_JOB(v_Cmd.OWNER||'.'||v_Cmd.OBJECT_NAME, true);
ELSIF v_Cmd.OBJECT_TYPE = 'PROGRAM' then
DBMS_SCHEDULER.DROP_PROGRAM(v_Cmd.OWNER||'.'||v_Cmd.OBJECT_NAME, true);
else
execute immediate v_Cmd.dropcmd;
end if;
dbms_output.put_line(v_Cmd.dropcmd);
exception
when others then
null; -- ignore errors
end;
end loop;
SELECT count(*)
INTO v_ItemCount
FROM ALL_OBJECTS AO
WHERE AO.OWNER = '&USER'
AND AO.OBJECT_TYPE NOT IN ('INDEX','LOB')
AND AO.OBJECT_NAME NOT LIKE 'BIN$%';
end loop;
execute immediate 'purge dba_recyclebin';
end;

Here is the script
begin
for i in (select * from dba_objects where owner = 'SO' and object_type <> 'TABLE')
loop
execute immediate 'drop ' || i.object_type || ' ' || i.object_name;
end loop;
for j in (select * from dba_objects where owner = 'SO' and object_type = 'TABLE')
loop
execute immediate 'drop ' || j.object_type || ' ' || j.object_name || ' cascade constraints';
end loop;
end;
/
This link provides details about how to create shell script or batch script to run sql scripts.
https://oracle-base.com/articles/misc/oracle-shell-scripting

Here we go i have compiled a shell to perform the Purge Schema. Let me know if this helps.
#weekly report
#!/bin/ksh
export ORACLE_HOME=/opt/oracle/product/1020
export PATH=$ORACLE_HOME/bin:$PATH:.
export NLS_LANG=AMERICAN_AMERICA.AL32UTF8
ABC=`sqlplus -s <username>/<password>#<sid> <<+
set sqlbl on;
set serveroutput on;
DECLARE
v_ItemCount INTEGER;
BEGIN
SELECT COUNT(*)
INTO v_ItemCount
FROM ALL_OBJECTS AO
WHERE AO.OWNER = '&USER'
AND AO.OBJECT_TYPE NOT IN ('INDEX', 'LOB')
AND AO.OBJECT_NAME NOT LIKE 'BIN$%';
WHILE (v_ItemCount > 0)
LOOP
FOR v_Cmd IN
(SELECT 'drop '
|| AO.OBJECT_TYPE
|| ' '
|| '"'
||AO.OWNER
||'"'
|| '.'
|| '"'
||AO.OBJECT_NAME
||'"'
|| DECODE(AO.OBJECT_TYPE, 'TABLE', ' CASCADE CONSTRAINTS', '') AS DROPCMD,
AO.OWNER,
AO.OBJECT_TYPE,
AO.OBJECT_NAME
FROM ALL_OBJECTS AO
WHERE AO.OWNER = '&USER'
AND AO.OBJECT_TYPE NOT IN ('INDEX', 'LOB')
AND AO.OBJECT_NAME NOT LIKE 'BIN$%'
)
LOOP
BEGIN
IF v_Cmd.OBJECT_TYPE = 'SCHEDULE' THEN
DBMS_SCHEDULER.DROP_SCHEDULE(v_Cmd.OWNER||'.'||v_Cmd.OBJECT_NAME, true);
ELSIF v_Cmd.OBJECT_TYPE = 'JOB' THEN
DBMS_SCHEDULER.DROP_JOB(v_Cmd.OWNER||'.'||v_Cmd.OBJECT_NAME, true);
ELSIF v_Cmd.OBJECT_TYPE = 'PROGRAM' THEN
DBMS_SCHEDULER.DROP_PROGRAM(v_Cmd.OWNER||'.'||v_Cmd.OBJECT_NAME, true);
ELSE
EXECUTE immediate v_Cmd.dropcmd;
END IF;
dbms_output.put_line(v_Cmd.dropcmd);
EXCEPTION
WHEN OTHERS THEN
NULL; -- ignore errors
END;
END LOOP;
SELECT COUNT(*)
INTO v_ItemCount
FROM ALL_OBJECTS AO
WHERE AO.OWNER = '&USER'
AND AO.OBJECT_TYPE NOT IN ('INDEX','LOB')
AND AO.OBJECT_NAME NOT LIKE 'BIN$%';
END LOOP;
EXECUTE immediate 'purge dba_recyclebin';
END;
/
exit
+`

Related

How can I change this script to skip views?

These statements list all of the columns that contain a certain. Is is also searching views and I need to know how to skip views.
DECLARE
match_count integer;
v_search_string varchar2(4000) := 'FE/Operational';
BEGIN
FOR t IN (SELECT owner,
table_name,
column_name
FROM all_tab_columns
WHERE data_type in ('VARCHAR2','VARCHAR','NCHAR','NVARCHAR2') )
LOOP
BEGIN
EXECUTE IMMEDIATE
'SELECT COUNT(*) FROM '||t.owner || '.' || t.table_name||
' WHERE '||t.column_name||' = :1'
INTO match_count
USING v_search_string;
IF match_count > 0 THEN
dbms_output.put_line( t.owner || '.' || t.table_name ||'
'||t.column_name||' '||match_count );
END IF;
EXCEPTION
WHEN others THEN
dbms_output.put_line( 'Error encountered trying to read ' ||
t.column_name || ' from ' ||
t.owner || '.' || t.table_name );
dbms_output.put_line(SQLERRM);
END;
END LOOP;
END;
/
You can use the ALL_OBJECT view for including only Tables as follows:
SELECT
ATT.OWNER,
ATT.TABLE_NAME,
ATT.COLUMN_NAME
FROM
ALL_TAB_COLUMNS ATT JOIN ALL_OBJECTS AO
ON ATT.TABLE_NAME = AO.OBJECT_NAME AND ATT.OWNER = AO.OWNER
WHERE
DATA_TYPE IN (
'VARCHAR2',
'VARCHAR',
'NCHAR',
'NVARCHAR2'
)
AND AO.OBJECT_TYPE = 'TABLE'
You could join your query on all_objects and check that the object type is a table:
SELECT owner,
table_name,
column_name
FROM all_tab_columns atc
JOIN all_object ao ON atc.table_name = ao.object_name AND
atc.owner = ao.owner
WHERE data_type IN ('VARCHAR2', 'VARCHAR', 'NCHAR', 'NVARCHAR2') AND
object_type = 'TABLE'

Flow does not enter in the loop

Below is a PL/SQL. The problem is that the flow does not enter loop. I am unable to figure out what is the problem. Both the queries return results, i.e the query in the loop and the query within the loop does return results.
DECLARE
p_file_name VARCHAR2(4000) :='GHCPExtract_100_005_2011052218000700.csv';
v_file_name VARCHAR2(4000) :='' || '''' || p_file_name ||'''';
v_count NUMBER :=0;
v_loop NUMBER :=0;
begin
DBMS_OUTPUT.PUT_LINE( 'BEFORE LOOP');
FOR C IN (
SELECT
S.SOURCE_TRX_KEY_SEGMENT1 ,
S.SOURCE_TRX_KEY_SEGMENT2 ,
S.SOURCE_TRX_KEY_SEGMENT3 ,
S.SOURCE_TRX_KEY_SEGMENT4 ,
S.SOURCE_TRX_KEY_SEGMENT5 ,
S.SOURCE_TRX_KEY_SEGMENT6
FROM DM_RS.STG_GHCP_EXTRACT S
WHERE S.SOURCE_FILE_NAME = v_file_name
)
LOOP
DBMS_OUTPUT.PUT_LINE( 'IN LOOP');
BEGIN
select
1
into
v_count
from
(
select
T.SOURCE_TRX_KEY_SEGMENT1 ,
T.SOURCE_TRX_KEY_SEGMENT2 ,
T.SOURCE_TRX_KEY_SEGMENT3 ,
T.SOURCE_TRX_KEY_SEGMENT4 ,
T.SOURCE_TRX_KEY_SEGMENT5 ,
T.SOURCE_TRX_KEY_SEGMENT6
from
GTT_SEGMENT_ID t
WHERE 1=1
AND
T.SOURCE_TRX_KEY_SEGMENT1 = C.SOURCE_TRX_KEY_SEGMENT1
and
T.SOURCE_TRX_KEY_SEGMENT2 = C.SOURCE_TRX_KEY_SEGMENT2
and
T.SOURCE_TRX_KEY_SEGMENT3 = C.SOURCE_TRX_KEY_SEGMENT3
and
T.SOURCE_TRX_KEY_SEGMENT4 = C.SOURCE_TRX_KEY_SEGMENT4
and
T.SOURCE_TRX_KEY_SEGMENT5 = C.SOURCE_TRX_KEY_SEGMENT5
and
T.SOURCE_TRX_KEY_SEGMENT6 = C.SOURCE_TRX_KEY_SEGMENT6
and t.source_file_name = v_file_name
);
EXCEPTION
WHEN NO_DATA_FOUND THEN
v_count := 0;
END;
IF (v_count = 1)
THEN
V_LOOP := V_LOOP +1;
END IF;
END LOOP;
DBMS_OUTPUT.PUT_LINE( V_LOOP);
END;
DBMS_OUTPUT
BEFORE LOOP
0
v_file_name VARCHAR2(4000) :='' || '''' || p_file_name ||'''';
....
WHERE S.SOURCE_FILE_NAME = '' || '''' || v_file_name ||''''
How many quotes contain SOURCE_FILE_NAME in table?
You can check count rows in query using temporary variable -
SELECT count(1)
INTO cnt_
FROM DM_RS.STG_GHCP_EXTRACT S
WHERE S.SOURCE_FILE_NAME = '''' || v_file_name ||'''';
DBMS_OUTPUT.put_line('rows count: '||cnt_);
Also, check that data in table commited and available in other session.
EDIT:
It's obvious that query in cycle returns no rows, because 'IN LOOP' doesn't printed in output.
How many rows return this query? -
SELECT count(1)
FROM dm_rs.stg_ghcp_extract s
WHERE s.source_file_name = '' || '''' || 'GHCPExtract_100_005_2011052218000700.csv' || ''''
Btw, if you need only count of compared rows you can use one select with EXISTS instead of cycle.
SELECT count(1)
into v_count
FROM dm_rs.stg_ghcp_extract c
WHERE c.source_file_name = v_file_name
AND EXISTS
(SELECT 1
FROM gtt_segment_id t
WHERE t.source_trx_key_segment1 = c.source_trx_key_segment1
AND t.source_trx_key_segment2 = c.source_trx_key_segment2
AND t.source_trx_key_segment3 = c.source_trx_key_segment3
AND t.source_trx_key_segment4 = c.source_trx_key_segment4
AND t.source_trx_key_segment5 = c.source_trx_key_segment5
AND t.source_trx_key_segment6 = c.source_trx_key_segment6
AND t.source_file_name = c.source_file_name)

oracle delete selected objects from schema

I want to delete objects from schema and I have stored all the objects those should not be deleted in a table, all other objects which are not in the table should be deleted. I have following code which is not working can some one help me here. I have searched the net there is only one thing "DELETE ALL OBJECT" which is not relevant to my case.
DECLARE
stringa VARCHAR2(100);
stringb VARCHAR2(1000) := '';
CURSOR cur IS
SELECT *
FROM user_objects;
BEGIN
FOR c IN cur LOOP
BEGIN
stringa := '';
BEGIN
SELECT tab_name
INTO stringb
FROM zretain_list;
IF c.object_type = 'VIEW' THEN
stringa := 'drop view '
|| c.object_name;
EXECUTE IMMEDIATE stringa;
ELSIF c.object_type = 'TABLE'
AND c.object_name != stringb THEN
stringa := 'drop table "'
|| c.object_name
|| '" cascade constraints';
EXECUTE IMMEDIATE stringa;
ELSIF c.object_type = 'SEQUENCE' THEN
stringa := 'drop sequence '
|| c.object_name;
EXECUTE IMMEDIATE stringa;
ELSIF c.object_type = 'PACKAGE' THEN
stringa := 'drop package '
|| c.object_name;
EXECUTE IMMEDIATE stringa;
ELSIF c.object_type = 'TRIGGER' THEN
stringa := 'drop trigger '
|| c.object_name;
EXECUTE IMMEDIATE stringa;
ELSIF c.object_type = 'PROCEDURE' THEN
stringa := 'drop procedure '
|| c.object_name;
EXECUTE IMMEDIATE stringa;
ELSIF c.object_type = 'FUNCTION' THEN
stringa := 'drop function '
|| c.object_name;
EXECUTE IMMEDIATE stringa;
ELSIF c.object_type = 'SYNONYM' THEN
stringa := 'drop synonym '
|| c.object_name;
EXECUTE IMMEDIATE stringa;
ELSIF c.object_type = 'INDEX' THEN
stringa := 'drop index '
|| c.object_name;
EXECUTE IMMEDIATE stringa;
ELSIF c.object_type = 'PACKAGE BODY' THEN
stringa := 'drop PACKAGE BODY '
|| c.object_name;
EXECUTE IMMEDIATE stringa;
ELSIF c.object_type = 'DATABASE LINK' THEN
stringa := 'drop database link '
|| c.object_name;
EXECUTE IMMEDIATE stringa;
END IF;
EXCEPTION
WHEN OTHERS THEN
NULL;
-- PURGE recyclebin
END;
END;
END LOOP;
END;
/
You can start with something like
begin
For i in (select object_type, object_name from user_objects o
where object_name != 'ZRETAIN_LIST' and object_type in ('TABLE', 'FUNCTION', 'PROCEDURE', 'PACKAGE', 'SEQUENCE' )
and not exists ( select 1 from zretain_list where tab_name = o.object_name ) )
loop
execute immediate 'drop '||i.object_type || ' ' || i.object_name ;
end loop;
end;
/

How to trim all columns in all rows in all tables of type string?

In Oracle 10g, is there a way to do the following in PL/SQL?
for each table in database
for each row in table
for each column in row
if column is of type 'varchar2'
column = trim(column)
Thanks!
Of course, doing large-scale dynamic updates is potentially dangerous and time-consuming. But here's how you can generate the commands you want. This is for a single schema, and will just build the commands and output them. You could copy them into a script and review them before running. Or, you could change dbms_output.put_line( ... ) to EXECUTE IMMEDIATE ... to have this script execute all the statements as they are generated.
SET SERVEROUTPUT ON
BEGIN
FOR c IN
(SELECT t.table_name, c.column_name
FROM user_tables t, user_tab_columns c
WHERE c.table_name = t.table_name
AND data_type='VARCHAR2')
LOOP
dbms_output.put_line(
'UPDATE '||c.table_name||
' SET '||c.column_name||' = TRIM('||c.column_name||') WHERE '||
c.column_name||' <> TRIM('||c.column_name||') OR ('||
c.column_name||' IS NOT NULL AND TRIM('||c.column_name||') IS NULL)'
);
END LOOP;
END;
Presumably you want to do this for every column in a schema, not in the database. Trying to do this to the dictionary tables would be a bad idea...
declare
v_schema varchar2(30) := 'YOUR_SCHEMA_NAME';
cursor cur_tables (p_schema_name varchar2) is
select owner, table_name, column_name
from all_tables at,
inner join all_tab_columns atc
on at.owner = atc.owner
and at.table_name = atc.table_name
where atc.data_type = 'VARCHAR2'
and at.owner = p_schema;
begin
for r_table in cur_tables loop
execute immediate 'update ' || r.owner || '.' || r.table_name
|| ' set ' || r.column_name || ' = trim(' || r.column_name ||');';
end loop;
end;
This will only work for fields that are VARCHAR2s in the first place. If your database contains CHAR fields, then you're out of luck, because CHAR fields are always padded to their maximum length.

How to drop all user tables?

How can I drop all user tables in oracle?
I have problem with constraints. When I disable all it is still no possible.
BEGIN
FOR cur_rec IN (SELECT object_name, object_type
FROM user_objects
WHERE object_type IN
('TABLE',
'VIEW',
'MATERIALIZED VIEW',
'PACKAGE',
'PROCEDURE',
'FUNCTION',
'SEQUENCE',
'SYNONYM',
'PACKAGE BODY'
))
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;
FOR cur_rec IN (SELECT *
FROM all_synonyms
WHERE table_owner IN (SELECT USER FROM dual))
LOOP
BEGIN
EXECUTE IMMEDIATE 'DROP PUBLIC SYNONYM ' || cur_rec.synonym_name;
END;
END LOOP;
END;
/
If you just want a really simple way to do this.. Heres a script I have used in the past
select 'drop table '||table_name||' cascade constraints;' from user_tables;
This will print out a series of drop commands for all tables in the schema. Spool the result of this query and execute it.
Source: https://forums.oracle.com/forums/thread.jspa?threadID=614090
Likewise if you want to clear more than tables you can edit the following to suit your needs
select 'drop '||object_type||' '|| object_name || ';' from user_objects where object_type in ('VIEW','PACKAGE','SEQUENCE', 'PROCEDURE', 'FUNCTION', 'INDEX')
Another answer that worked for me is (credit to http://snipt.net/Fotinakis/drop-all-tables-and-constraints-within-an-oracle-schema/)
BEGIN
FOR c IN (SELECT table_name FROM user_tables) LOOP
EXECUTE IMMEDIATE ('DROP TABLE "' || c.table_name || '" CASCADE CONSTRAINTS');
END LOOP;
FOR s IN (SELECT sequence_name FROM user_sequences) LOOP
EXECUTE IMMEDIATE ('DROP SEQUENCE ' || s.sequence_name);
END LOOP;
END;
Note that this works immediately after you run it. It does NOT produce a script that you need to paste somewhere (like other answers here). It runs directly on the DB.
begin
for i in (select 'drop table '||table_name||' cascade constraints' tbl from user_tables)
loop
execute immediate i.tbl;
end loop;
end;
The simplest way is to drop the user that owns the objects with the cascade command.
DROP USER username CASCADE
SELECT 'DROP TABLE "' || TABLE_NAME || '" CASCADE CONSTRAINTS;'
FROM user_tables;
user_tables is a system table which contains all the tables of the user
the SELECT clause will generate a DROP statement for every table
you can run the script
The easiest way would be to drop the tablespace then build the tablespace back up. But I'd rather not have to do that. This is similar to Henry's except that I just do a copy/paste on the resultset in my gui.
SELECT
'DROP'
,object_type
,object_name
,CASE(object_type)
WHEN 'TABLE' THEN 'CASCADE CONSTRAINTS;'
ELSE ';'
END
FROM user_objects
WHERE
object_type IN ('TABLE','VIEW','PACKAGE','PROCEDURE','FUNCTION','SEQUENCE')
To remove all objects in oracle :
1) Dynamic
DECLARE
CURSOR IX IS
SELECT * FROM ALL_OBJECTS WHERE OBJECT_TYPE ='TABLE'
AND OWNER='SCHEMA_NAME';
CURSOR IY IS
SELECT * FROM ALL_OBJECTS WHERE OBJECT_TYPE
IN ('SEQUENCE',
'PROCEDURE',
'PACKAGE',
'FUNCTION',
'VIEW') AND OWNER='SCHEMA_NAME';
CURSOR IZ IS
SELECT * FROM ALL_OBJECTS WHERE OBJECT_TYPE IN ('TYPE') AND OWNER='SCHEMA_NAME';
BEGIN
FOR X IN IX LOOP
EXECUTE IMMEDIATE('DROP '||X.OBJECT_TYPE||' SCHEMA_NAME.'||X.OBJECT_NAME|| ' CASCADE CONSTRAINT');
END LOOP;
FOR Y IN IY LOOP
EXECUTE IMMEDIATE('DROP '||Y.OBJECT_TYPE||' SCHEMA_NAME.'||Y.OBJECT_NAME);
END LOOP;
FOR Z IN IZ LOOP
EXECUTE IMMEDIATE('DROP '||Z.OBJECT_TYPE||' SCHEMA_NAME.'||Z.OBJECT_NAME||' FORCE ');
END LOOP;
END;
/
2)Static
SELECT 'DROP TABLE "' || TABLE_NAME || '" CASCADE CONSTRAINTS;' FROM user_tables
union ALL
select 'drop '||object_type||' '|| object_name || ';' from user_objects
where object_type in ('VIEW','PACKAGE','SEQUENCE', 'PROCEDURE', 'FUNCTION')
union ALL
SELECT 'drop '
||object_type
||' '
|| object_name
|| ' force;'
FROM user_objects
WHERE object_type IN ('TYPE');
Please follow the below steps.
begin
for i in (select 'drop table '||table_name||' cascade constraints' tb from user_tables)
loop
execute immediate i.tb;
end loop;
commit;
end;
purge RECYCLEBIN;

Resources