SQL 如何将oracle sql结果输出到Windows中的文件中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15253440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 14:06:02  来源:igfitidea点击:

How to output oracle sql result into a file in windows?

sqloraclesqlplus

提问by Dreamer

I tried

我试过

select * from users 
save D:\test.sql create;

But SQL plus gives me "no proper ended" How to specify path in oracle sql in windows?

但是SQL plus给了我“没有适当的结束”如何在windows中的oracle sql中指定路径?

回答by Marc

Use the spool:

使用线轴:

spool myoutputfile.txt
select * from users;
spool off;

Note that this will create myoutputfile.txt in the directory from which you ran SQL*Plus.

请注意,这将在您运行 SQL*Plus 的目录中创建 myoutputfile.txt。

If you need to run this from a SQL file (e.g., "tmp.sql") when SQLPlus starts up and output to a file named "output.txt":

如果您需要在 SQLPlus 启动时从 SQL 文件(例如“tmp.sql”)运行它并输出到名为“output.txt”的文件:

tmp.sql:

tmp.sql:

select * from users;

Command:

命令:

sqlplus -s username/password@sid @tmp.sql > output.txt

Mind you, I don't have an Oracle instance in front of me right now, so you might need to do some of your own work to debug what I've written from memory.

请注意,我现在面前没有 Oracle 实例,因此您可能需要自己做一些工作来调试我从内存中编写的内容。

回答by John D

Very similar to Marc, only difference I would make would be to spool to a parameter like so:

与 Marc 非常相似,我唯一要做的区别是将其假脱机为这样的参数:

WHENEVER SQLERROR EXIT 1
SET LINES 32000
SET TERMOUT OFF ECHO OFF NEWP 0 SPA 0 PAGES 0 FEED OFF HEAD OFF TRIMS ON TAB OFF
SET SERVEROUTPUT ON

spool &1

-- Code

spool off
exit

And then to call the SQLPLUS as

然后将 SQLPLUS 称为

sqlplus -s username/password@sid @tmp.sql /tmp/output.txt

回答by Shine

spool "D:\test\test.txt"

select  
  a.ename  
from  
  employee a  
inner join department b  
on  
(  
  a.dept_id = b.dept_id  
)  
;  
spool off  

This query will spool the sql result in D:\test\test.txt

此查询将在 D:\test\test.txt 中假脱机 sql 结果

回答by Mohamed Dernoun

just to make the Answer 2 much easier, you can also define the folder where you can put your saved file

只是为了使答案 2 更容易,您还可以定义可以放置保存文件的文件夹

    spool /home/admin/myoutputfile.txt
    select * from table_name;
    spool off;

after that only with nano or vi myoutputfile.txt, you will see all the sql track.

之后只用nano 或vi myoutputfile.txt,你就会看到所有的sql 轨迹。

hope is that help :)

希望有帮助:)

回答by Dudi Boy

Having the same chore on windows 10, and windows server 2012. I found the following solution:

在 Windows 10 和 Windows Server 2012 上有同样的苦差事。我找到了以下解决方案:

echo quit |sqlplus schemaName/schemaPassword@sid @plsqlScript.sql > outputFile.log

Explanation

解释

echo quit |send the quit command to exit sqlplusafter the script completes

echo quit |sqlplus脚本完成后发送退出命令退出

sqlplus schemaName/schemaPassword@sid @plsqlScript.sqlexecute plssql script plsqlScript.sqlin schema schemaNamewith password schemaPasswordconnecting to SID sid

sqlplus schemaName/schemaPassword@sid @plsqlScript.sql使用连接到 SID 的密码plsqlScript.sql在模式中执行 plssql 脚本schemaNameschemaPasswordsid

> outputFile.logredirect sqlplus output to log file outputFile.log

> outputFile.log将 sqlplus 输出重定向到日志文件 outputFile.log