14

I have a series of queries that I want to output to .csv files. The only tool I have to query the database is SQL Developer.

I could run each query and then use the Export dialogue in SQL Developer to write them to files, but that's cumbersome, particularly when this needs to be done for multiple files every day.

This works for some people Directly export a query to CSV using SQL Developer

But it doesn't work for me.

For example, if I try

spool "C:\Users\james.foreman\Downloads\Temp\myfile.csv"

select distinct placement_type
FROM jf_placements;

spool off;

then in the script output pane of SQL Developer, I see

Cannot create SPOOL file C:\Users\james.foreman\Downloads\Temp\myfile.csv

and although myfile.csv is created, there's no results. (There are two rows returned by the query.)

My first thought was that there was a permissions issue writing to C:\Users\james.foreman\Downloads\Temp but that doesn't appear to be the case, because if I delete the myfile.csv and then run the SQL, the myfile.csv file is recreated, but it never has anything in it.

So I assume this is a configuration issue, either with the Windows machine I'm running SQL Developer on, or with my SQL Developer set up. Where should I look to investigate further?

@Devolus 's answer to Procedure to export table to multiple csv files includes the instruction "In the SQL Window right click -> Change Window to -> Command Window" but if I right click on the SQL Window, I don't see a Change Window option.

(Running Windows 7, SQL Developer Version 4.0.2.15, Build 15.21, database is Oracle 11.2)

5
  • Can you try the same from SQL*Plus and see if this time the file has content or not. Commented Sep 24, 2014 at 5:46
  • Hi, I can't; I only have SQL Developer on this machine and not SQL*Plus.
    – JamesF
    Commented Sep 24, 2014 at 7:57
  • See my answer. Regarding SQL*Plus, I just wanted you to verify. No issues, just follow the answer. Commented Sep 24, 2014 at 8:27
  • I can't see any way it can both create the file and say it didn't; I can't duplicate this with the same version you have. Are you sure that's the entire script you're executing and the script, message and actual directory path are copied and pasted - not retyped which might have changed and hidden something? (The command window thing seems to really be talking about PL/SQL Developer, which is a different product).
    – Alex Poole
    Commented Sep 24, 2014 at 9:17
  • INSTALL ORACLE CLIENT, to generate spools for sql developer..hope this will work bcoz it works for me...
    – ketan
    Commented Aug 5, 2017 at 7:37

5 Answers 5

17

When you run your script, press "Run Script" instead of "Run Statment" or you may press F5.

It fixed my problem, hope it can fix yours too

2
  • Thanks,my problem fixed after I highlight the whole script and press F5 using Oracle SQL developer . Commented Jun 12, 2019 at 18:00
  • Oh big thanks. It fixed a problem I have been fixing all day!
    – Sviatlana
    Commented Apr 14, 2020 at 14:53
6

The fact that the file is created, but has no data, perhaps, the last statement, SPOOL OFF is not yet executed. Add a new line in the script and try again.

For example, your script would look like :

    spool "C:\Users\james.foreman\Downloads\Temp\myfile.csv"

    select distinct placement_type
    FROM jf_placements
    /

    spool off
    /

-- need a new line to make sure spool off executes
3
  • Thanks - that now has the SQL query in the result file. Although I see from other questions around this that changing to running from a script should then solve for that.
    – JamesF
    Commented Sep 24, 2014 at 10:06
  • Thanks for the feedback. Yes, you can put the content inside a .sql script and execute the script. Commented Sep 24, 2014 at 10:17
  • 1
    In 'Oracle SQL Developer' run the statement as script i.e. press "Run Script" instead of "Run Statment" or you may press F5. Commented Feb 16, 2018 at 7:48
4

My problem solved after I highlight the whole script and press F5 in SQL developer .

1

In my case, it took two things to fix the problem:

  • running from a script (using @C:\file.sql notation)
  • upgrading to the latest version of SQL Developer

I was running SQL Developer 3.2.20.09 (doesn't work), and now use 4.0.2.51 (exact same script works).

Also,

0

Wonderful,my problem solved , First i cleared all queries in sql developer page window (where i am running the queries) and kept only the spool comand which i want to run after selection (ctrl+A) and clicking F5, ran and content also got generated in a file,where initially only empty file alone getting generated

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.