我有以下Select查询,它将生成删除语句以删除模式中的所有对象。
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';
我想创建批处理文件,并假设我可以创建可能是游标或其他内容而不是生成删除语句,并将其保存在批处理文件中并运行批处理文件以删除架构中的所有内容。怎么做?
答案 0 :(得分:1)
这是脚本
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;
/
此链接提供有关如何创建shell脚本或批处理脚本以运行sql脚本的详细信息。
https://oracle-base.com/articles/misc/oracle-shell-scripting
答案 1 :(得分:1)
您可以在Internet上找到许多脚本。它们都不是100%工作。我们可以有各种各样的陷阱。像调度程序链或物化视图组一样。
这是我使用的那个(它的灵感来自我在互联网上找到的那个)
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;
答案 2 :(得分:1)
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
+`