Ao usar o SQLcl para consultar o banco de dados Oracle, você pode usar o
SET SQLFORMAT comando para determinar o formato dos resultados. Você também pode usar comentários embutidos para especificar o formato diretamente em sua consulta.
Por exemplo, você pode usar o seguinte para gerar os resultados no formato CSV:
SET SQLFORMAT csv;
SELECT * FROM regions; Ou você pode fazer assim:
SELECT /*csv*/ * FROM regions; Opções de formatação
As seguintes opções podem ser usadas para especificar o formato de saída:
| Opção | Descrição |
|---|---|
default | Limpa toda a formatação e a define como formatação de estilo SQL*PLUS. Esta opção só funciona com o SET SQLFORMAT comando. |
ansiconsole | Formatação avançada com base nos dados e no tamanho do terminal. |
fixed | Largura fixa. |
csv | Formato separado por vírgulas com strings entre aspas duplas (" ). |
loader | Canal (| ) formato delimitado com strings entre aspas duplas (" ). |
delimited | Formato CSV com separador opcional, caixa esquerda e direita. |
text | Exibe os resultados como texto, sem separadores. Esta opção não está documentada em HELP opção. |
insert | Gera SQL INSERT declarações dos resultados. |
json | Formato JSON correspondente ao formato de coleção ORDS. |
json-formatted | Formato JSON combinando com o formato de coleção ORDS e bem impresso. O JSON é apresentado em um formato mais legível. |
xml | Formato XML. |
html | Formato tabular HTML. Gera o código para um documento HTML com uma tabela com estilo e uma ferramenta de pesquisa JavaScript. |
Exemplos
Abaixo estão exemplos para demonstrar as opções acima.
default
Limpa toda a formatação e a define para a formatação estilo SQL*PLUS.
SET SQLFORMAT default;
SELECT * FROM regions; Resultado:
SQL Format Cleared
REGION_ID REGION_NAME
---------- -------------------------
1 Europe
2 Americas
3 Asia
4 Middle East and Africa ansiconsole
Formatação avançada com base em dados e tamanho do terminal.
SET SQLFORMAT ansiconsole;
SELECT * FROM regions; Resultado:
REGION_ID REGION_NAME
____________ _________________________
1 Europe
2 Americas
3 Asia
4 Middle East and Africa fixed
Largura fixa.
SET SQLFORMAT fixed;
SELECT * FROM regions; Resultado:
"REGION_ID" "REGION_NAME" "1" "Europe" "2" "Americas" "3" "Asia" "4" "Middle East and Africa"
csv
Formato separado por vírgulas com strings entre aspas duplas (
" ). SET SQLFORMAT csv;
SELECT * FROM regions; Resultado:
"REGION_ID","REGION_NAME" 1,"Europe" 2,"Americas" 3,"Asia" 4,"Middle East and Africa"
loader
Canal (
| ) formato delimitado com strings entre aspas duplas (" ). SET SQLFORMAT loader;
SELECT * FROM regions; Resultado:
1|"Europe"| 2|"Americas"| 3|"Asia"| 4|"Middle East and Africa"|
delimited
Formato CSV com separador opcional, gabinete esquerdo e direito. Isso permite que você escolha seus próprios delimitadores.
SET SQLFORMAT delimited , < >;
SELECT * FROM regions; Resultado:
<REGION_ID>,<REGION_NAME> 1,<Europe> 2,<Americas> 3,<Asia> 4,<Middle East and Africa>
text
Exibe os resultados como texto, sem separadores. Esta opção não está documentada em
HELP opção. SET SQLFORMAT text;
SELECT * FROM regions; Resultado:
"REGION_ID"null"REGION_NAME" 1null"Europe" 2null"Americas" 3null"Asia" 4null"Middle East and Africa"
insert
Gera SQL
INSERT declarações dos resultados. SET SQLFORMAT insert;
SELECT * FROM regions; Resultado:
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 correspondente ao formato de coleção ORDS.
SET SQLFORMAT json;
SELECT * FROM regions; Resultado:
{"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 combinando com o formato de coleção ORDS e bem impresso. O JSON é apresentado em um formato mais legível.
SET SQLFORMAT json-formatted;
SELECT * FROM regions; Resultado:
{
"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; Resultado:
<?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 tabular HTML. Gera o código para um documento HTML com uma tabela com estilo e uma ferramenta de pesquisa JavaScript.
SET SQLFORMAT html;
SELECT * FROM regions; O código HTML resultante é bastante grande, pois cria um documento HTML, adiciona estilos, JavaScript, etc.
Aqui está a aparência do HTML resultante quando salvo em um
.html arquivo e renderizado em um navegador:regions.html
E aqui está o código HTML real que foi gerado:
<!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>