如何使用Oracle SQL Developer中的spool命令将查询结果输出到CSV文件

时间:2014-10-17 18:27:13

标签: sql oracle-sqldeveloper spool

我正在尝试使用Oracle SQL Developer中的SPOOL命令将SQL查询的结果输出到CSV文件。

我可以通过将其作为脚本调用来输出简单查询的结果。这是有效的查询:

spool trivial_output.csv
select /*csv*/ * from trivial_table;
spool off;

这就是我成功召集它的方式(F5):

@'C:\Spool to CSV\trivial_query.sql'

然而,当我尝试使用稍微复杂的查询完全相同的东西时,我收到错误消息: “SQL错误:ORA-00933:SQL命令未正确结束00933. 00000 - SQL命令未正确结束”

spool total_records.csv
select  /*csv*/     enrol.year, enrol.college, count(*) as "Total Records"
from        enrolment enrol
inner join  regis_status_type regstatus
on          enrol.regis_status_type_id = regstatus.regis_status_type_id
where       enrol.year in ('201213', '201314')
and         regstatus.regis_status_type_code in ('10','41')
group by    enrol.year, enrol.college
order by    enrol.year, enrol.college
spool off;

2 个答案:

答案 0 :(得分:1)

我只需要添加一个分号来将SQL * Plus命令与SQL语句分开。感谢Justin Cave

spool total_records.csv
select  /*csv*/     enrol.year, enrol.college, count(*) as "Total Records"
from        enrolment enrol
inner join  regis_status_type regstatus
on          enrol.regis_status_type_id = regstatus.regis_status_type_id
where       enrol.year in ('201213', '201314')
and         regstatus.regis_status_type_code in ('10','41')
group by    enrol.year, enrol.college
order by    enrol.year, enrol.college;
spool off;

答案 1 :(得分:0)

spool "D:\test\test.txt"  

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

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