如何在我的本地文件夹中生成假脱机文件
How to generate spool file in my local folder
我想生成一个带文件名的假脱机文件,并将其存储到我可以在代码本身中提供其路径的本地文件夹中。怎么做?假设下面是我希望在单独的 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 名称,是否也需要删除父名称 table。如果是,如何一次删除table_name和parent_table_name?(根据代码)。
如果你是从 PL/SQL 开始的,那么你可以 set serveroutput on
和 spool
到本地文件(但也会得到一些 垃圾, 即代码本身):
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 目录对象, 通常 指向那里).但是你会得到一个不错的文件。
请参阅 '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 文件 --
我想生成一个带文件名的假脱机文件,并将其存储到我可以在代码本身中提供其路径的本地文件夹中。怎么做?假设下面是我希望在单独的 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 名称,是否也需要删除父名称 table。如果是,如何一次删除table_name和parent_table_name?(根据代码)。
如果你是从 PL/SQL 开始的,那么你可以 set serveroutput on
和 spool
到本地文件(但也会得到一些 垃圾, 即代码本身):
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 目录对象, 通常 指向那里).但是你会得到一个不错的文件。
请参阅 '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 文件 --