Export Oracle Query Results to an HTML File when using SQLcl

When using SQLcl with Oracle Database, you can use the SPOOL command to export your query results to a file with an .html extension, and you can set SQLFORMAT to html in order to output the actual query results in HTML format.

Example

Here’s an example to demonstrate:

SET SQLFORMAT html;
SPOOL '/Users/barney/data/regions.html';
SELECT * FROM regions;
SPOOL off;
SET SQLFORMAT ansiconsole;

Here’s what it did, line by line:

  • The first line sets SQLFORMAT to html. This ensures that our resulting .html file does in fact contain HTML code.
  • The second line uses the SPOOL command to specify where the output file will be written. Be sure to change /Users/barney/data/regions.html to a location on your system, and an appropriate file name.
  • On the third line, I ran the SQL query – the results for which I’m exporting. In this case, I exported the whole regions table.
  • Next, I turned SPOOL off.
  • Lastly, I set SQLFORMAT back to my original setting, which was ansiconsole. This is optional – you could leave it at json if you prefer, or change it to something else.

Here’s what the resulting file looks like:

And here’s the source code behind that file:

<!DOCTYPE html>
<html>

<head>
  <meta charset='UTF-8'>
  
  <title>Result Data</title>
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  
  <style>
  * { 
    margin: 0; 
    padding: 0; 
  }
  body { 
    font: 14px/1.4 Palatino, Serif; 
  }
  
  /* 
  Generic Styling, for Desktops/Laptops 
  */
  table { 
    width: 100%; 
    border-collapse: collapse; 
  }
  /* Zebra striping */
  tr:nth-of-type(odd) { 
    background: #eee; 
  }
  th { 
    background: #333; 
    color: white; 
    font-weight: bold; 
  }
  td, th { 
    padding: 6px; 
    border: 1px solid #9B9B9B; 
    text-align: left; 
  }
  @media 
  only screen and (max-width: 760px),
  (min-device-width: 768px) and (max-device-width: 1024px)  {
    table, thead, tbody, th, td, tr { display: block; }
    thead tr { position: absolute;top: -9999px;left: -9999px;}
    tr { border: 1px solid #9B9B9B; }
    td { border: none;border-bottom: 1px solid #9B9B9B; position: relative;padding-left: 50%; }
    
    td:before { position: absolute;top: 6px;left: 6px;width: 45%; padding-right: 10px; white-space: nowrap;}
    
    /*
    Label the data
    */
td:nth-of-type(1):before { content: "REGION_ID"; }
td:nth-of-type(2):before { content: "REGION_NAME"; }
  }
  
  /* Smartphones (portrait and landscape) ----------- */
  @media only screen
  and (min-device-width : 320px)
  and (max-device-width : 480px) {
    body { 
      padding: 0; 
      margin: 0; 
      width: 320px; }
    }
  
  /* iPads (portrait and landscape) ----------- */
  @media only screen and (min-device-width: 768px) and (max-device-width: 1024px) {
    body { 
      width: 495px; 
    }
  }
  
  </style>
  <!--<![endif]-->
<script type="text/javascript">

function search(){
  
  var s = document.getElementById('search').value;

  rows = document.getElementById('data').getElementsByTagName('TR');
  for(var i=0;i<rows.length;i++){
    if ( rows[i].textContent.indexOf(s)>0  || s.length==0 ) {
	  rows[i].style.display ='';
    } else {
      rows[i].style.display ='none';
    }
  }
}


var timer;
function delayedSearch() {
	clearTimeout(timer);
	console.log('delay-ing')
    timer = setTimeout(function () {
		console.log('delay-running')
		search();
    }, 500);
  }</script>
</head>

<body>
<div><input type="text" size="30" maxlength="1000" value="" id="search" onkeyup="delayedSearch();" /><input type="button" value="Go" onclick="lsearch();"/> </div>
<table><thead><tr>	<th>REGION_ID</th>
	<th>REGION_NAME</th>
</tr></thead>
<tbody id="data">

	<tr>
<td align="right">1</td>
<td>Europe</td>
	</tr>
	<tr>
<td align="right">2</td>
<td>Americas</td>
	</tr>
	<tr>
<td align="right">3</td>
<td>Asia</td>
	</tr>
	<tr>
<td align="right">4</td>
<td>Middle East and Africa</td>
	</tr>
</tbody></table><!-- SQL:
SELECT * FROM regions--></body></html>

4 rows selected.

So it generates the whole HTML document – not just the table.

You’ll notice that some CSS has been added for styling purposes, and JavaScript has been added to create a search function.

Remove Feedback

You can remove the X rows selected with SET FEEDBACK off:

SET SQLFORMAT html;
SET FEEDBACK off;
SPOOL '/Users/barney/data/regions_feedback_off.html';
SELECT * FROM regions;
SPOOL off;
SET FEEDBACK on;
SET SQLFORMAT ansiconsole;

In this case I turned FEEDBACK back on after exporting the file.