Export Timed Query Results From SQL*Plus

Published: Jan 15, 2019
Updated: May 3, 2021

At work, we have to SSH into an ubuntu box in order to run queries. Since this box has no GUI, all work must be done through command line and SQL*Plus.

Sadly, pasting multi-line queries into SQL*Plus is a no-go. And copying query results, if they’re more than a few dozen lines, isn’t much better. So, after trial and error, here’s my solution:

Here’s an example query file. Note, the query must end with a semicolon.

SPOOL output.txt
TIMING START timer

<query_goes_here>

TIMING STOP timer
SPOOL OFF

And an example SCP command.

 scp <username>@<host>:/path/to/output.txt ~/Desktop