How can I store the result of a SQL query in a CSV file using Squirrel?

alex picture alex · Feb 22, 2010 · Viewed 45.7k times · Source

Version 3.0.3. It's a fairly large result-set, around 3 million rows.

Answer

Rod picture Rod · Oct 17, 2012

Martin pretty much has this right.

The TL/DR version is that you need the "SQLScripts" plugin (which is one of the "standard' plugins), and then you can select these menu options: Session > Scripts > Store Result of SQL in File

I'm looking at version 3.4. I don't know when this feature was introduced but you may need to upgrade if you don't have and cannot install the SQLScripts plugin.

Instructions for installing a new plugin can be found at: http://squirrel-sql.sourceforge.net/user-manual/quick_start.html#plugins

But if you're performing a fresh install of Squirrel you can simply select the "SQLScripts" plugin during the installation.

Here's the long version:


  1. Run the query

    Connect to the database. Click on the SQL tab. Enter your query. Hit the run button (or Ctrl-Enter).

    You should see the first 100 rows or so in the results area in the bottom half of the pane (depending upon how you've configured the Limit Rows option).

  2. Export the full results

    Open the Session menu. Select the Scripts item (nearly at the bottom of this long menu). Select Store Result of SQL in File.

    This opens a dialog box where you can configure your export. Make sure you check Export the complete result set to get everything.


I haven't tried this with a 3 million row result set, but I have noticed that Squirrel seems to stream the data to disk (rather than reading it all into memory before writing), so I don't see any reason why it wouldn't work with an arbitrarily large file.

Note that you can export directly to a file by using Ctrl-T to invoke the tools popup and selecting sql2file.