如何将细胞假脱机到不同的文件?
How to spool cells to different files?
是否可以将 table 的不同行的内容假脱机到不同的文件?
我的 table 的行数不固定,它有这些列:
身份证,姓名,CONTENT_TO_SPOOL
我想每行创建一个输出文件,将 CONTENT_TO_SPOOL 假脱机到名为 NAME.txt
的文件
这是我想要实现的一些伪代码:
FOR row IN SELECT * FROM MyTABLE LOOP
spool row.NAME.txt
dbms_output.put_line(row.CONTENT_TO_SPOOL);
spool OFF
END LOOP;
我意识到 SPOOL
是 SQL PLUS 的一部分,而 FOR LOOP
是 PL/SQL 结构,所以我的伪代码不起作用。
我无法写入服务器,因此 UTL_FILE
不可能。
有没有一种方法可以遍历 SQL PLUS 中的行并使用 SPOOL
将内容保存到不同的文件?
伪代码并没有说清楚你想要什么,但是这里......
此方法的工作原理是从数据中生成一个脚本,以针对每个不同的名称使用嵌入式假脱机命令重新查询数据。
set lines 200
set trimspool on
set head off pages 0
-- may need more set stuff to ensure output is clean
spool run_query.sql
select distinct 'spool ' || name || '.txt' || chr(10) ||
'select content_to_spol from mytable where name = ''' || name || ''';' || chr(10) ||
'spool off' cmd
from mytable;
spool off
@run_query.sql
我能想到的一种方法是 SQL Plus。不好,但它有效:1. 创建具有以下内容的 script.sql
set serveroutput on
spool step1.log
declare
begin
for rec in (select rownum as id, col_a, col_b from mytable)
loop
dbms_output.put_line('spool file_'||rec.id||'.log');
dbms_output.put_line('select '''||rec.col_a|| ''' as vchar_col , '||rec.col_b|| ' as num_col from dual;');
dbms_output.put_line('spool off');
end loop;
end;
/
spool off
@script.sql;
现在您的文件 step1.log 包含以下内容:
假脱机 file_1.log
select 'a' as vchar_col , 4 as num_col from dual;
关闭线轴
线轴 file_2.log
select 'b' as vchar_col , 44 as num_col from dual;
关闭
PL/SQL 程序成功完成。
- 删除最后一行(PL/SQL程序成功完成)并执行@step1.log
- file_1.log 和 file_2.log 等被创建,每个包含一行 table mytable
是否可以将 table 的不同行的内容假脱机到不同的文件?
我的 table 的行数不固定,它有这些列: 身份证,姓名,CONTENT_TO_SPOOL
我想每行创建一个输出文件,将 CONTENT_TO_SPOOL 假脱机到名为 NAME.txt
的文件这是我想要实现的一些伪代码:
FOR row IN SELECT * FROM MyTABLE LOOP
spool row.NAME.txt
dbms_output.put_line(row.CONTENT_TO_SPOOL);
spool OFF
END LOOP;
我意识到 SPOOL
是 SQL PLUS 的一部分,而 FOR LOOP
是 PL/SQL 结构,所以我的伪代码不起作用。
我无法写入服务器,因此 UTL_FILE
不可能。
有没有一种方法可以遍历 SQL PLUS 中的行并使用 SPOOL
将内容保存到不同的文件?
伪代码并没有说清楚你想要什么,但是这里......
此方法的工作原理是从数据中生成一个脚本,以针对每个不同的名称使用嵌入式假脱机命令重新查询数据。
set lines 200
set trimspool on
set head off pages 0
-- may need more set stuff to ensure output is clean
spool run_query.sql
select distinct 'spool ' || name || '.txt' || chr(10) ||
'select content_to_spol from mytable where name = ''' || name || ''';' || chr(10) ||
'spool off' cmd
from mytable;
spool off
@run_query.sql
我能想到的一种方法是 SQL Plus。不好,但它有效:1. 创建具有以下内容的 script.sql
set serveroutput on
spool step1.log
declare
begin
for rec in (select rownum as id, col_a, col_b from mytable)
loop
dbms_output.put_line('spool file_'||rec.id||'.log');
dbms_output.put_line('select '''||rec.col_a|| ''' as vchar_col , '||rec.col_b|| ' as num_col from dual;');
dbms_output.put_line('spool off');
end loop;
end;
/
spool off
@script.sql;
现在您的文件 step1.log 包含以下内容:
假脱机 file_1.log
select 'a' as vchar_col , 4 as num_col from dual;
关闭线轴
线轴 file_2.log
select 'b' as vchar_col , 44 as num_col from dual;
关闭
PL/SQL 程序成功完成。
- 删除最后一行(PL/SQL程序成功完成)并执行@step1.log
- file_1.log 和 file_2.log 等被创建,每个包含一行 table mytable