2

我想在 Oracle 11g 中使用假脱机功能。

我希望将整个输出假脱机到一个文件中,并将输出的子集假脱机到一个单独的文件中。

在下面的示例中,我想temp_1.txt包含来自 A、B、C、D 和 E 的数据

temp_2.txt我只想要 D 的数据。

sqlplus user/pass@inst

spool on temp_1.txt    
select * from A;
select * from B;
select * from C; 
spool on temp_2.txt
select * from D;
spool off temp_2.txt
select * from E;

exit;

注意:-由于这是非常旧的遗留代码,我无法为 D 编写单独的 sqlplus 会话或重新排序选择。

4

2 回答 2

3

在 sqlplus 脚本中完成这一切怎么样。如果您曾经在不同的系统(即 Microsoft Windows)上运行,则需要更改主机命令。但是,他们也需要在 shell 脚本中进行更改。

spool all_queries.txt
select * from A;
select * from B;
select * from C;
spool off

spool only_d_query.txt
select * from D;
spool off

host cat only_d_query.txt >>all_queries.txt

spool all_queries.txt append
select * from E;
spool off
于 2014-08-18T16:10:39.177 回答
2

你不能那样做。SPOOL命令一次只允许打开一个文件;您的第二个命令spool temp_2.txt(没有on)将在打开并开始写入第二个文件之前关闭第一个文件。并且off不采用任何进一步的参数。

Usage: SPOOL { <file> | OFF | OUT }
where <file> is file_name[.ext] [CRE[ATE]|REP[LACE]|APP[END]]

一种解决方案是将语句的输出假脱机到不同的文件:

spool temp_1.txt
select * from A;
select * from B;
select * from C;
spool temp_2.txt
select * from D;
spool temp_3.txt
select * from E;
spool off

...然后将操作系统中的所有三个文件合并为一个,以获取您的“主”输出文件,同时仍单独保留仅 D 文件。例如:

cat temp_2.txt >> temp_1.txt
cat temp_3.txt >> temp_1.txt
rm temp_3.txt`

如果我理解正确的话,哪个会留下你想要temp_1.txt的内容。temp_2.txt当然,如果您在 Windows 上使用不同的方法。

或者,您可以在 PL/SQL 块中运行查询,UTL_FILE并将结果写入两个打开文件中的一个或两个。但这需要更多的工作,并且会将文件写入服务器 - 因此您需要对DIRECTORY要写入的对象具有权限,并需要访问指向的底层文件系统目录才能检索文件。

于 2014-01-31T12:28:28.810 回答