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

48,267

Solution 1

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.

Solution 2

I have found a way to do this, there is a nice support for this in Squirrel. Run the SQL select (the 100 row limit will be ignored by the exporter, don't worry). Then, in the main menu, choose Session, Scripts, Store Result of SQL in File. This functionality may not be present by default, it may be present in some standard plugin (but not installed by default). I don't know which plugin though.

Solution 3

I also wanted to export results of SQL query to CSV file using SquirrelSQL. However according to changes file it seems that this functionality is not supported even in SquirrelSql 3.3.0.

So far I was able to export only data shown in 'result table' of SQL query by right click on the table > Export to CSV. The table size is by default 100 rows and so is the CSV export. You may change the table size in Session Properties > SQL > Limit rows. E.g. change the size to 10000 and your export will also contain 10000 rows. The question is, how will SquirrelSql deal with really big result sets (millions of rows)...

Solution 4

Run from your GUI:

COPY (SELECT * FROM some_table) TO '/some/path/some_table.csv' WITH CSV HEADER

Share:
48,267
alex
Author by

alex

Updated on August 04, 2020

Comments

  • alex
    alex over 3 years

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