我需要将循环的输出假脱机到文件。我的代码如下:
set termout off
set echo off
set colsep ''
set linesize 5000
set heading off
set feedback off
set preformat off
set trimspool on
set serverout on
spool 'C:\folder\script.sql'
begin
for rec in (select unique prgm_id from tmp_table_output) loop
for rec2 in (select unique cmpg_id from tmp_table_output where prgm_id = rec.prgm_id) loop
dbms_output.put_line('spool ''C:\folder\PRG''||lpad('||rec.prgm_id||', 4, 0)||''_CMPG''||lpad('||rec2.cmpg_id||', 4, 0)||''.txt'' CREATE');
dbms_output.put_line('SELECT field FROM tmp_table_output where prgm_id = '||rec.prgm_id||' and cmpg_id = '||rec2.cmpg_id);
dbms_output.put_line('spool off');
end loop;
end loop;
end;
/
spool off;
但是,当我检查script.sql文件时,输出是:
而不是实际迭代循环。begin
for rec in (select unique prgm_id from tmp_table_output) loop
for rec2 in (select unique cmpg_id from tmp_table_output where prgm_id = rec.prgm_id) loop
dbms_output.put_line('spool ''C:\RemoteOnboarding\PRG''||lpad('||rec.prgm_id||', 4, 0)||''_CMPG''||lpad('||rec2.cmpg_id||', 4, 0)||''.txt'' CREATE');
dbms_output.put_line('SELECT field FROM tmp_unica_output where prgm_id = '||rec.prgm_id||' and cmpg_id = '||rec2.cmpg_id);
dbms_output.put_line('spool off');
end loop;
end loop;
end;
/
spool off;
如何让它将循环输出写入文件?
答案 0 :(得分:2)
SPOOL
是一个SQL * Plus命令。如果要从PL / SQL写入文件,请使用UTL_FILE
包。
答案 1 :(得分:0)
如果您可以将BEGIN和END块之间的代码更改为单个SQL,它可能会对您有所帮助。
这里有一个如何操作的示例,代码未经过测试,因为我没有完成您的所有设置,也可能存在一些语法错误,如果您收到任何错误,请尝试告诉我< / p>
set termout off
set echo off
set colsep ''
set linesize 5000
set heading off
set feedback off
set preformat off
set trimspool on
set serverout on
spool 'C:\folder\script.sql'
select text from (
select unique 1 as rn, rec.prgm_id, rec2.cmpg_id, 'spool ''C:\folder\PRG''||lpad('||rec.prgm_id||', 4, 0)||''_CMPG''||lpad('||rec2.cmpg_id||', 4, 0)||''.txt'' CREATE' as text
from tmp_table_output rec, tmp_table_output rec2
where a.prgm_id = b.prgm_id
union all
select 2, rec.prgm_id, rec2.cmpg_id, 'SELECT field FROM tmp_table_output where prgm_id = '||rec.prgm_id||' and cmpg_id = '||rec2.cmpg_id
from tmp_table_output rec, tmp_table_output rec2
where a.prgm_id = b.prgm_id
union all
select unique 3, rec.prgm_id, rec2.cmpg_id, 'spool off'
from tmp_table_output rec, tmp_table_output rec2
where a.prgm_id = b.prgm_id
)
order by prgm_id, cmpg_id, rn;
spool off;