SQLPlus 动态假脱机文件名
SQLPlus dynamic spool filename
我有一个 Oracle 数据库,我想将数据导出到 file.However 文件名,扩展名和分隔符将从 table 中获取值。问题是我无法使用 table 中的值。你能建议我一个方法吗?或者如果我可以批量执行此操作?
Table(id, 路径, 文件名, 扩展名, 分隔符)
script.sql
conn ....
variable fullpath varchar2(20);
variable filename varchar2(10);
variable extension varchar2(5);
variable sep varchar2(1);
begin
select filename, path, extension,separator
into :filename, :fullpath, :extension, :sep
from Table;
end;
/
set separator sep
spool fullpath||filename||'.'||extension;
... select queries...
spool off;
此致
SPOOL 是 SQLPlus 命令,因此您不能在 PlSQL 块中动态使用它。
一种方法是在 运行 时创建第二个脚本,根据您的查询动态构建,然后 运行 它来完成这项工作。
例如:
conn ...
set serveroutput on
set feedback off
variable fullpath varchar2(20);
variable filename varchar2(10);
variable extension varchar2(5);
variable sep varchar2(1);
/* spool to a fixed file, that will contain your dynamic script */
spool d:\secondScript.sql
begin
select 'filename', 'd:\', 'txt', '|'
into :filename, :fullpath, :extension, :sep
from dual;
/* write the second script */
dbms_output.put_line('set colsep ' || :sep);
dbms_output.put_line('spool ' || :fullpath || :filename || '.' || :extension);
dbms_output.put_line('select 1, 2, 3 from dual;');
dbms_output.put_line('spool off');
end;
/
spool off
/* run the second script */
@d:\secondscript.sql
这给出:
SQL> sta C:\firstScript.sql
Connected.
set colsep |
spool d:\filename.txt
select 1, 2, 3 from dual;
1| 2| 3
----------|----------|----------
1| 2| 3
d:\filename.txt:
1| 2| 3
----------|----------|----------
1| 2| 3
您可以使用替换变量和 the new_value
clause of the column
command。
conn ....
column spool_path new_value sub_spool_path noprint
column sep new_value sub_sep noprint
set verify off
set termout off
select path || filename ||'.'|| extension as spool_path, separator as sep
from Table;
set termout on
set separator &sub_sep
spool &sub_spool_path
... select queries...
spool off;
我有一个 Oracle 数据库,我想将数据导出到 file.However 文件名,扩展名和分隔符将从 table 中获取值。问题是我无法使用 table 中的值。你能建议我一个方法吗?或者如果我可以批量执行此操作?
Table(id, 路径, 文件名, 扩展名, 分隔符)
script.sql
conn ....
variable fullpath varchar2(20);
variable filename varchar2(10);
variable extension varchar2(5);
variable sep varchar2(1);
begin
select filename, path, extension,separator
into :filename, :fullpath, :extension, :sep
from Table;
end;
/
set separator sep
spool fullpath||filename||'.'||extension;
... select queries...
spool off;
此致
SPOOL 是 SQLPlus 命令,因此您不能在 PlSQL 块中动态使用它。
一种方法是在 运行 时创建第二个脚本,根据您的查询动态构建,然后 运行 它来完成这项工作。 例如:
conn ...
set serveroutput on
set feedback off
variable fullpath varchar2(20);
variable filename varchar2(10);
variable extension varchar2(5);
variable sep varchar2(1);
/* spool to a fixed file, that will contain your dynamic script */
spool d:\secondScript.sql
begin
select 'filename', 'd:\', 'txt', '|'
into :filename, :fullpath, :extension, :sep
from dual;
/* write the second script */
dbms_output.put_line('set colsep ' || :sep);
dbms_output.put_line('spool ' || :fullpath || :filename || '.' || :extension);
dbms_output.put_line('select 1, 2, 3 from dual;');
dbms_output.put_line('spool off');
end;
/
spool off
/* run the second script */
@d:\secondscript.sql
这给出:
SQL> sta C:\firstScript.sql
Connected.
set colsep |
spool d:\filename.txt
select 1, 2, 3 from dual;
1| 2| 3
----------|----------|----------
1| 2| 3
d:\filename.txt:
1| 2| 3
----------|----------|----------
1| 2| 3
您可以使用替换变量和 the new_value
clause of the column
command。
conn ....
column spool_path new_value sub_spool_path noprint
column sep new_value sub_sep noprint
set verify off
set termout off
select path || filename ||'.'|| extension as spool_path, separator as sep
from Table;
set termout on
set separator &sub_sep
spool &sub_spool_path
... select queries...
spool off;