Quantcast
Channel: csv | ThatJeffSmith
Viewing all articles
Browse latest Browse all 29

Quick Tip: Spooling to ‘Excel’

$
0
0

Problem: I have 3 queries I want to run. I want the end result to be a single spreadsheet that has all the query results included.

This problem may sound familiar to you, I have talked about how to do this with the GUI here – but you get one workbook per exported TABLE.

The problem with my previous ‘solution’ is that you would need to code your queries to database VIEWs and then export those.

Here’s a quick and dirty way to get everything you want to an ‘excel’ file. You’ll get a CSV file, which you can then open in Excel and convert if you’d like.

My queries are simple, number of rows are small – to make the post simple, but you can substitute your stuff and should be A-OK.

cd c:\users\jdsmith
SET sqlformat csv
SET feedback off
SET echo off
spool blog_queries_excel.csv
SELECT * FROM regions;
SELECT * FROM locations;
SELECT * FROM departments;
spool off

Execute this code in SQL Developer with F5 or in SQLcl..and your output will look like so, when opened in Excel:

Ta-da.

What does the code do?

cd tells us where to read and write for working with files (it changes the SQLPath essentially)

set sqlformat csv tells us to take the ouptut and put it through our formatter, using the csv style.

set feedback off tells us to suppress messages like ’27 rows selected’ or ‘table created’

set echo off tells us not to include the query that was executed in the output

spool tells us to copy the output to a file

If you want to suppress the output in the console or script output panel and JUST write to the file, then do this

But Jeff, I want lines between my tables…

Then change the script…turn feedback back on, or use PROMPTs or simply select the whitespace as desired into the output.

I hope this helps!

End of day, when you’re done with your file, and you’re in Excel, you’ll start cleaning it up IN EXCEL. All of this I’ve just shown you is just a kickstarter to get the data into the file that much faster.


Viewing all articles
Browse latest Browse all 29

Trending Articles