在 Oracle 中使用批处理文件删除架构中的所有对象

Delete all objects in schema using batch file in Oracle

我有下面的 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';

我想创建批处理文件,假设我可以创建游标或其他东西,而不是单独生成删除语句,并将其保存在批处理文件中,运行 批处理文件删除架构中的所有内容。怎么做?

这是脚本

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;
/

此 link 提供了有关如何创建 shell 脚本或批处理脚本到 运行 sql 脚本的详细信息。

https://oracle-base.com/articles/misc/oracle-shell-scripting

您可以在 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;
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
+`