4

I am trying to output the results of a SQL query to a CSV file using the SPOOL command in Oracle SQL Developer.

I am able to output the results of a trivial query by calling it as a script. Here is the query that worked:

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

And this is how I successfully called it (F5):

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

However, when I attempt the exact same thing with a slightly more complex query, I get the error message: "SQL Error: ORA-00933: SQL command not properly ended 00933. 00000 - SQL command not properly ended"

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;
5
  • You probably need some hard coded commas in your select clause. In fact, may as well do it properly and have some hard coded double quotes as well in case any of the colleges contain a comma. Commented Oct 17, 2014 at 18:39
  • I'm not sure if it matters, but the Oracle error message says the error is triggered just before the final line ("spool off"). So the problem seems to occur later than the SELECT statement. Commented Oct 17, 2014 at 18:43
  • 1
    It appears that you are missing the semicolon after your SELECT statement in the second example. That causes Oracle to interpret the spool off as the last line of your SQL statement rather than as a separate SQL*Plus command. That, in turn, causes your SQL statement to be syntactically invalid. Commented Oct 17, 2014 at 20:35
  • Thanks, @justin-cave. Are you saying that there should be a semicolon after the ORDER BY clause? I will try your suggestion on Monday when I am at work. Commented Oct 18, 2014 at 19:45
  • You're right... I needed to add a semi-colon after the ORDER BY clause. Commented Oct 20, 2014 at 12:57

2 Answers 2

3

I just needed to add a semi-colon to separate the SQL*Plus command from the SQL statement. Thanks to 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;
Sign up to request clarification or add additional context in comments.

Comments

-1
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 here D:\test\test.txt

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.