Quando si utilizza SQLcl per interrogare il database Oracle, è possibile utilizzare SET SQLFORMAT comando per determinare il formato dei risultati.
Puoi anche utilizzare i commenti in linea per specificare il formato direttamente all'interno della tua query.
Ad esempio, puoi utilizzare quanto segue per generare i risultati in formato CSV:
SET SQLFORMAT csv;
SELECT * FROM regions; Oppure puoi farlo in questo modo:
SELECT /*csv*/ * FROM regions; Opzioni di formattazione
È possibile utilizzare le seguenti opzioni per specificare il formato di output:
| Opzione | Descrizione |
|---|---|
default | Cancella tutta la formattazione e la imposta sulla formattazione in stile SQL*PLUS. Questa opzione funziona solo con SET SQLFORMAT comando. |
ansiconsole | Formattazione avanzata basata su dati e dimensioni del terminale. |
fixed | Larghezza fissa. |
csv | Formato separato da virgole con stringhe racchiuse tra virgolette (" ). |
loader | Pipa (| ) formato delimitato con stringhe racchiuse tra virgolette (" ). |
delimited | Formato CSV con separatore opzionale, allegato sinistro e destro. |
text | Emette i risultati come testo, senza separatori. Questa opzione non è documentata in HELP opzione. |
insert | Genera SQL INSERT dichiarazioni dai risultati. |
json | Formato JSON corrispondente al formato di raccolta ORDS. |
json-formatted | Formato JSON corrispondente al formato di raccolta ORDS e ben stampato. Il JSON è presentato in un formato più leggibile dall'uomo. |
xml | Formato XML. |
html | Formato tabellare HTML. Genera il codice per un documento HTML con una tabella con uno stile e uno strumento di ricerca JavaScript. |
Esempi
Di seguito sono riportati esempi per dimostrare le opzioni di cui sopra.
default
Cancella tutta la formattazione e imposta la formattazione in stile SQL*PLUS.
SET SQLFORMAT default;
SELECT * FROM regions; Risultato:
SQL Format Cleared
REGION_ID REGION_NAME
---------- -------------------------
1 Europe
2 Americas
3 Asia
4 Middle East and Africa ansiconsole
Formattazione avanzata basata su dati e dimensioni del terminale.
SET SQLFORMAT ansiconsole;
SELECT * FROM regions; Risultato:
REGION_ID REGION_NAME
____________ _________________________
1 Europe
2 Americas
3 Asia
4 Middle East and Africa fixed
Larghezza fissa.
SET SQLFORMAT fixed;
SELECT * FROM regions; Risultato:
"REGION_ID" "REGION_NAME" "1" "Europe" "2" "Americas" "3" "Asia" "4" "Middle East and Africa"
csv
Formato separato da virgole con stringhe racchiuse tra virgolette (" ).
SET SQLFORMAT csv;
SELECT * FROM regions; Risultato:
"REGION_ID","REGION_NAME" 1,"Europe" 2,"Americas" 3,"Asia" 4,"Middle East and Africa"
loader
Tubo (| ) formato delimitato con stringhe racchiuse tra virgolette (" ).
SET SQLFORMAT loader;
SELECT * FROM regions; Risultato:
1|"Europe"| 2|"Americas"| 3|"Asia"| 4|"Middle East and Africa"|
delimited
Formato CSV con separatore opzionale, contenitore sinistro e destro. Ciò ti consente di scegliere i tuoi delimitatori.
SET SQLFORMAT delimited , < >;
SELECT * FROM regions; Risultato:
<REGION_ID>,<REGION_NAME> 1,<Europe> 2,<Americas> 3,<Asia> 4,<Middle East and Africa>
text
Emette i risultati come testo, senza separatori. Questa opzione non è documentata in HELP opzione.
SET SQLFORMAT text;
SELECT * FROM regions; Risultato:
"REGION_ID"null"REGION_NAME" 1null"Europe" 2null"Americas" 3null"Asia" 4null"Middle East and Africa"
insert
Genera SQL INSERT dichiarazioni dai risultati.
SET SQLFORMAT insert;
SELECT * FROM regions; Risultato:
REM INSERTING into REGIONS SET DEFINE OFF; Insert into REGIONS (REGION_ID,REGION_NAME) values (1,'Europe'); Insert into REGIONS (REGION_ID,REGION_NAME) values (2,'Americas'); Insert into REGIONS (REGION_ID,REGION_NAME) values (3,'Asia'); Insert into REGIONS (REGION_ID,REGION_NAME) values (4,'Middle East and Africa');
json
Formato JSON corrispondente al formato di raccolta ORDS.
SET SQLFORMAT json;
SELECT * FROM regions; Risultato:
{"results":[{"columns":[{"name":"REGION_ID","type":"NUMBER"},{"name":"REGION_NAME","type":"VARCHAR2"}],"items":
[
{"region_id":1,"region_name":"Europe"}
,{"region_id":2,"region_name":"Americas"}
,{"region_id":3,"region_name":"Asia"}
,{"region_id":4,"region_name":"Middle East and Africa"}
]}]} json-formatted
Formato JSON corrispondente al formato di raccolta ORDS e piuttosto stampato. Il JSON è presentato in un formato più leggibile dall'uomo.
SET SQLFORMAT json-formatted;
SELECT * FROM regions; Risultato:
{
"results" : [
{
"columns" : [
{
"name" : "REGION_ID",
"type" : "NUMBER"
},
{
"name" : "REGION_NAME",
"type" : "VARCHAR2"
}
],
"items" : [
{
"region_id" : 1,
"region_name" : "Europe"
},
{
"region_id" : 2,
"region_name" : "Americas"
},
{
"region_id" : 3,
"region_name" : "Asia"
},
{
"region_id" : 4,
"region_name" : "Middle East and Africa"
}
]
}
]
} xml
Formato XML.
SET SQLFORMAT xml;
SELECT * FROM regions; Risultato:
<?xml version='1.0' encoding='UTF-8' ?> <RESULTS> <ROW> <COLUMN NAME="REGION_ID"><![CDATA[1]]></COLUMN> <COLUMN NAME="REGION_NAME"><![CDATA[Europe]]></COLUMN> </ROW> <ROW> <COLUMN NAME="REGION_ID"><![CDATA[2]]></COLUMN> <COLUMN NAME="REGION_NAME"><![CDATA[Americas]]></COLUMN> </ROW> <ROW> <COLUMN NAME="REGION_ID"><![CDATA[3]]></COLUMN> <COLUMN NAME="REGION_NAME"><![CDATA[Asia]]></COLUMN> </ROW> <ROW> <COLUMN NAME="REGION_ID"><![CDATA[4]]></COLUMN> <COLUMN NAME="REGION_NAME"><![CDATA[Middle East and Africa]]></COLUMN> </ROW> </RESULTS>
html
Formato tabellare HTML. Genera il codice per un documento HTML con una tabella con uno stile e uno strumento di ricerca JavaScript.
SET SQLFORMAT html;
SELECT * FROM regions; Il codice HTML risultante è piuttosto grande, poiché crea un documento HTML, aggiunge stili, JavaScript, ecc.
Ecco come appare l'HTML risultante quando viene salvato in un .html file e renderizzato in un browser:
regions.html
Ed ecco il codice HTML effettivo che è stato generato:
<!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>