【问题标题】:How to generate spool file in my local folder如何在我的本地文件夹中生成假脱机文件
【发布时间】:2021-11-08 16:57:03
【问题描述】:

我想生成一个带有文件名的假脱机文件并将其存储到我可以在代码本身中提供的路径的本地文件夹中。怎么做?假设下面是我希望在单独的 SQL 文件中生成脚本的代码

declare
 lv_str varchar2(1000);
begin
for c in(select distinct a.table_name as table_name, 
                b.table_name as parent_table_name ,
                a.owner
           from all_constraints a, 
                all_constraints b 
          where a.r_constraint_name = b.constraint_name 
            and a.constraint_type   = 'R'
            and b.constraint_type   = 'P'
            and a.owner             = b.owner   -- if parent and child belongs to the same schema
            and a.owner             = 'SCOTT'
       ) loop
 lv_str :='DROP TABLE ' || c.owner || '.' || c.table_name || '; -- ' || c.parent_table_name;
 dbms_output.put_Line (lv_str );
 end loop;
end;

/

另外,一个小帮助如果我想删除表名,是否也需要删除父表。如果是,如何一次删除 table_name 和 parent_table_name?(根据代码)。

【问题讨论】:

    标签: plsql oracle-sqldeveloper plsqldeveloper


    【解决方案1】:

    查看“cd”命令,在 SQL Developer 中可用,它是 CLI 对应的 SQLcl。 SQLcl 类似于 SQLPlus,但更好。

    这也可以在 SQL Developer 中使用,只需通过 F5 执行即可。

    SQL> cd c:\users\jdsmith\desktop
    SQL> set serveroutput on
    SQL> spool so4.txt
    SQL> declare
      2   lv_str varchar2(1000);
      3  begin
      4  for c in(select distinct a.table_name as table_name,
      5                  b.table_name as parent_table_name ,
      6                  a.owner
      7             from all_constraints a,
      8                  all_constraints b
      9            where a.r_constraint_name = b.constraint_name
     10              and a.constraint_type   = 'R'
     11              and b.constraint_type   = 'P'
     12              and a.owner             = b.owner   -- if parent and child belongs to the same schema
     13              and a.owner             = 'HR'
     14         ) loop
     15   lv_str :='DROP TABLE ' || c.owner || '.' || c.table_name || '; -- ' || c.parent_table_name;
     16   dbms_output.put_Line (lv_str );
     17   end loop;
     18  end;
     19* /
    DROP TABLE HR.EMPLOYEES; -- EMPLOYEES
    DROP TABLE HR.DBMSHP_FUNCTION_INFO; -- DBMSHP_RUNS
    DROP TABLE HR.JOB_HISTORY; -- EMPLOYEES
    DROP TABLE HR.DBMSHP_PARENT_CHILD_INFO; -- DBMSHP_FUNCTION_INFO
    DROP TABLE HR.EMPLOYEES; -- JOBS
    DROP TABLE HR.JOB_HISTORY; -- JOBS
    DROP TABLE HR.JOB_HISTORY; -- DEPARTMENTS
    DROP TABLE HR.DEPARTMENTS; -- LOCATIONS
    DROP TABLE HR.LOCATIONS; -- COUNTRIES
    DROP TABLE HR.WINE_REVIEWS; -- WINE_VARIETIES
    DROP TABLE HR.DEPARTMENTS; -- EMPLOYEES
    DROP TABLE HR.COUNTRIES; -- REGIONS
    DROP TABLE HR.EMPLOYEES; -- DEPARTMENTS
    
    
    PL/SQL procedure successfully completed.
    
    SQL> spool off;
    SQL>
    

    如果我去看看我桌面上的so4.txt文件——

    【讨论】:

    • 您甚至不必使用“cd”。您可以将 spool 文件的完整路径放在“spool”命令行中“:spool c:\users\jdsmith\desktop\so4.txt。
    • 可以,但是写入多个文件时 cd 更容易。
    【解决方案2】:

    如果您是从 PL/SQL 执行此操作,那么您可以将 set serveroutput onspool 放入本地文件(但也会得到一些垃圾,即代码本身):

    SQL> spool a.sql
    SQL>
    SQL> begin
      2    for cur_r in (select tname from tab) loop
      3      dbms_output.put_line('desc ' || cur_r.tname);
      4    end loop;
      5  end;
      6  /
    desc DEPT
    desc EMP
    desc EMPLOYEES
    
    PL/SQL procedure successfully completed.
    
    SQL> spool off
    

    结果:

    SQL> $type a.sql
    SQL>
    SQL> begin
      2    for cur_r in (select tname from tab) loop
      3      dbms_output.put_line('desc ' || cur_r.tname);
      4    end loop;
      5  end;
      6  /
    desc DEPT
    desc EMP
    desc EMPLOYEES
    
    PL/SQL procedure successfully completed.
    
    SQL> spool off
    
    SQL>
    

    或者,使用UTL_FILE 包,但它会在数据库服务器上的目录中创建一个文件(因为您必须使用通常指向那里的Oracle 目录对象)。但是你会得到一个不错的文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-02
      • 1970-01-01
      • 2012-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多