TÓPICO 2 – RESTRICTING AND SORTING DATA

  • Subtópico 2.1 – Limit the rows that are retrieved by a query (Limitar as linhas retornadas por uma query)
  • Subtópico 2.2 – Sort the rows that are retrieved by a query (Ordenar as linhas retornadas por uma query)
  • Subtópico 2.3 – Use ampersand substitution to restrict and sort output at runtime (Usar o & para restringir e ordenar a saida em tempo de execução)

Neste post, falarei um pouco sobre o segundo tópico oficial da prova de SQL Básico (1Z0-051). Um pouco já foi nos tópicos anteriores sobre algumas funcionalidades abordadas neste tópico, como ordenação das linhas, e limitação de linhas retornadas. Farei uma recapitulação destes itens e também falarei sobre o caractere “&”, que serve como substituição de variáveis em nível de execução.

Ainda usaremos as tabelas CERTIFICACAO.TAB_EMPREGADOS e CERTIFICACAO.TAB_DEPARTAMENTOS como forma de exemplificar os conceitos aqui abordados.

SUBTÓPICO 2.1 – LIMITAR AS LINHAS RETORNADAS POR UMA QUERY

Basicamente, para limitar as linhas retornadas por uma query, nós precisamos filtrar as linhas que são realmente necessárias para o nosso objetivo, seja a geração de relatórios e etc. Para isso, usamos o operador WHERE. Exemplos:

[et_pb_dmb_code_snippet _builder_version=”4.0.6″ code=”IC0tYWJhaXhvLCBzZSBlc3RhIGV4aWJpbmRvIGFwZW5hcyBhcyBsaW5oYXMgcXVlIHBvc3N1ZW0gbyB2YWxvciBkYQogLS1jb2x1bmEgSURfRU1QIGlndWFsIGEgMTAwMAoKIFNFTEVDVCBJRF9FTVAgQVMgSURfRU1QUkVHQURPLAogU0FMQVJJTyAiU2Fsw6FyaW8gKFIkKSIsCiBEQVRBX0FETUlTU0FPCiBGUk9NIENFUlRJRklDQUNBTy5UQUJfRU1QUkVHQURPUwogV0hFUkUgSURfRU1QID4gMTAwMAoKIC0tYWJhaXhvLCBldSBidXNjbyBvcyBlbXByZWdhZG9zIHF1ZSBwb3NzdWFtIHNhbGFyaW8gZW50cmUgCiAtLVIkMTAwMCwwMCBlIFIkMjAwMCwwMC4gbGVtYnJhbmRvIHF1ZSBvIEJFVFdFRU4gw6kgaW5jbHVzaXZvCgogU0VMRUNUICogCiBGUk9NIENFUlRJRklDQUNBTy5UQUJfRU1QUkVHQURPUyAKIFdIRVJFIFNBTEFSSU8gQkVUV0VFTiAxMDAwIEFORCAyMDAwCiBPUkRFUiBCWSBJRF9FTVA=” hover_enabled=”0″]IC0tYWJhaXhvLCBzZSBlc3RhIGV4aWJpbmRvIGFwZW5hcyBhcyBsaW5oYXMgcXVlIHBvc3N1ZW0gbyB2YWxvciBkYQogLS1jb2x1bmEgSURfRU1QIGlndWFsIGEgMTAwMAoKIFNFTEVDVCBJRF9FTVAgQVMgSURfRU1QUkVHQURPLAogU0FMQVJJTyAiU2Fsw6FyaW8gKFIkKSIsCiBEQVRBX0FETUlTU0FPCiBGUk9NIENFUlRJRklDQUNBTy5UQUJfRU1QUkVHQURPUwogV0hFUkUgSURfRU1QID4gMTAwMAoKIC0tYWJhaXhvLCBldSBidXNjbyBvcyBlbXByZWdhZG9zIHF1ZSBwb3NzdWFtIHNhbGFyaW8gZW50cmUgCiAtLVIkMTAwMCwwMCBlIFIkMjAwMCwwMC4gbGVtYnJhbmRvIHF1ZSBvIEJFVFdFRU4gw6kgaW5jbHVzaXZvCgogU0VMRUNUICogCiBGUk9NIENFUlRJRklDQUNBTy5UQUJfRU1QUkVHQURPUyAKIFdIRVJFIFNBTEFSSU8gQkVUV0VFTiAxMDAwIEFORCAyMDAwCiBPUkRFUiBCWSBJRF9FTVA=[/et_pb_dmb_code_snippet]

Uma outra forma de limitar as linhas retornadas é usando a cláusula ROWNUM. Esta cláusula faz com que minha query retorne no máximo X linhas. Ela poderia ser útil, por exemplo, para exibir apenas uma amostra da tabela (que também pode ser feito com o SAMPLE). Exemplo:

[et_pb_dmb_code_snippet _builder_version=”4.0.6″ code=”IC0tc2UgZXUgZGVzZWphciBleGliaXIgYXBlbmFzIDEwIGxpbmhhcyBkYSBtaW5oYSB0YWJlbGEgZGUgCiAtLWVtcHJlZ2Fkb3MsIHBvc3NvIHVz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QUkVHQURPUwogV0hFUkUgcm93bnVtIDw9IDEwCiBPUkRFUiBCWSBTQUxBUklPIERFU0M=[/et_pb_dmb_code_snippet]

Quando se insere uma linha em determinada tabela, o Oracle atribui um ROWNUM a essa linha. Cada linha possui um ROWNUM único que é atribuído a cada nova linha inserida em determinada tabela. Caso eu execute a seguinte query:

[et_pb_dmb_code_snippet _builder_version=”4.0.6″ code=”IFNFTEVDVCBST1dOVU0sIAogSURfRU1QCiBGUk9NIENFUlRJRklDQUNBTy5UQUJfRU1QUkVHQURPUwrigKYgZWxhIHZhaSBtZSByZXRvcm5hciBhIG9yZGVtIHF1ZSBvcyBlbXByZWdhZG9zIGZvcmFtIHNhbHZvcyBuYSB0YWJlbGEu” hover_enabled=”0″]IFNFTEVDVCBST1dOVU0sIAogSURfRU1QCiBGUk9NIENFUlRJRklDQUNBTy5UQUJfRU1QUkVHQURPUwrigKYgZWxhIHZhaSBtZSByZXRvcm5hciBhIG9yZGVtIHF1ZSBvcyBlbXByZWdhZG9zIGZvcmFtIHNhbHZvcyBuYSB0YWJlbGEu[/et_pb_dmb_code_snippet]

Agora, voltando a query QU01. Ela executará da seguinte forma: Serão buscadas as 10 primeiras linhas, por ordem de inserção, da tabela CERTIFICACAO.TAB_EMPREGADOS, e depois esse subgrupo de 10 linhas será ordenado. Na verdade, não é isso que quero, mas sim os 10 maiores salários entre todos os empregados. Para atingir esse objetivo, eu devo alterar a minha query, para que fique da seguinte forma:

[et_pb_dmb_code_snippet _builder_version=”4.0.6″ code=”IFNFTEVDVCAqIEZST00gKAogU0VMRUNUICoKIEZST00gQ0VSVElGSUNBQ0FPLlRBQl9FTVBSRUdBRE9TCiBPUkRFUiBCWSBTQUxBUklPIERFU0MKICkKIFdIRVJFIFJPV05VTSA8PSAxMA==” hover_enabled=”0″]IFNFTEVDVCAqIEZST00gKAogU0VMRUNUICoKIEZST00gQ0VSVElGSUNBQ0FPLlRBQl9FTVBSRUdBRE9TCiBPUkRFUiBCWSBTQUxBUklPIERFU0MKICkKIFdIRVJFIFJPV05VTSA8PSAxMA==[/et_pb_dmb_code_snippet]

O que eu estou fazendo acima: Primeiro eu busco todos os empregados e ordeno de forma decrescente pelo salário, para depois filtrar os 10 registros, que já estarão ordenados do jeito que eu preciso. A query mais interna, neste caso, é chamada de Inline View. Ela funciona como uma VIEW temporária. Falaremos sobre Views em uma outra oportunidade.

SUBTÓPICO 2.2 – ORDENAR AS LINHAS RETORNADAS POR UMA QUERY

Para se ordenar as linhas retornadas por uma query, usa-se o ORDER BY (que vocês já devem estar bem familiarizados). Tomando como exemplos, queries usadas no post anterior:

[et_pb_dmb_code_snippet _builder_version=”4.0.6″ code=”IC0tb3JkZW5hbmRvIG8gcmVzdWx0YWRvIHBlbG8gaWQgZGUgY2FkYSBlbXByZWdhZG8sIG9yZGVuYW5kbyAKIC0tZGUgZm9ybWEgYXNjZW5kZW50ZS4KIC0tYSBwYWxhdnJhIEFTQyBwb2RlIHNlciBvbWl0aWRhLCBqw6EgcXVlIG9yZGVuYXIgZGUgZm9ybWEgCiAtLWFzY2VuZGVudGUgw6kgbyBkZWZhdWx0CgogU0VMRUNUICogCiBGUk9NIENFUlRJRklDQUNBTy5UQUJfRU1QUkVHQURPUwogT1JERVIgQlkgSURfRU1QIEFTQzsKCiAtLW9yZGVuYW5kbyBvIHJlc3VsdGFkbyBwZWxvIGlkIGRlIGNhZGEgZW1wcmVnYWRvLCBvcmRlbmFuZG8gZGUgCiAtLWZvcm1hIGRlc2NlbmRlbnRlCgogU0VMRUNUICogCiBGUk9NIENFUlRJRklDQUNBTy5UQUJfRU1QUkVHQURPUwogT1JERVIgQlkgSURfRU1QIERFU0M7CgogLS1hbyBpbnZlcyBkZSB1c2FyIG8gbm9tZSBkYXMgY29sdW5hcywgZSBwb3NzaXZlbCB1c2FyIGEgcG9zaWNhbyAKIC0tZGFzIGNvbHVuYXMgbmEgbGlzdGEgZGUgY29sdW5hcyBkbyBzZWxlY3QKIC0tYWJhaXhvIGV1IGVzdG91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U1QLAogTk9NRV9FTVAsCiBJREFERQogRlJPTSBDRVJUSUZJQ0FDQU8uVEFCX0VNUFJFR0FET1MKIE9SREVSIEJZIDIgREVTQzs=[/et_pb_dmb_code_snippet]

É importante mencionar que quando se usa a ordenação por nome de coluna, eu posso colocar nomes de colunas que não estão dentro da lista de colunas de meu SELECT (mas que obviamente existem na tabela). No exemplo abaixo, eu estou ordenando pelo salário, mas reparem que não busco a coluna SALARIO:

[et_pb_dmb_code_snippet _builder_version=”4.0.6″ code=”IFNFTEVDVCBJRF9FTVAsIAogTk9NRV9FTVAsCiBJREFERQogRlJPTSBDRVJUSUZJQ0FDQU8uVEFCX0VNUFJFR0FET1MKIE9SREVSIEJZIFNBTEFSSU8gREVTQw==” hover_enabled=”0″]IFNFTEVDVCBJRF9FTVAsIAogTk9NRV9FTVAsCiBJREFERQogRlJPTSBDRVJUSUZJQ0FDQU8uVEFCX0VNUFJFR0FET1MKIE9SREVSIEJZIFNBTEFSSU8gREVTQw==[/et_pb_dmb_code_snippet]

SUBTÓPICO 2.3 – USAR O & PARA RESTRINGIR E ORDENAR A SAÍDA EM TEMPO DE EXECUÇÃO

No SQL*Plus, podemos usar o “e comercial”, ou “&” para trabalhar com variáveis e dinamicamente fornecer valores para definir filtros e colunas a serem buscadas. Abaixo é possível ver um exemplo bem simples. Para executa-lo, basta acessar o SQL*Plus, digitando no terminal o comando “sqlplus“, ou caso o ORACLE_HOME não esteja definido na variável PATH, digitar “ORACLE_HOME/bin/sqlplus”, onde ORACLE_HOME é o diretório de instalação do Oracle:

[et_pb_dmb_code_snippet _builder_version=”4.0.6″ code=”IFNRTD4gU0VMRUNUIElEX0VNUCwgTk9NRV9FTVAsIFNBTEFSSU8sIEZST00gQ0VSVElGSUNBQ0FPLlRBQl9FTVBSRUdBRE9TIFdIRVJFIElEX0VNUCA9ICZJRDs=” hover_enabled=”0″]IFNRTD4gU0VMRUNUIElEX0VNUCwgTk9NRV9FTVAsIFNBTEFSSU8sIEZST00gQ0VSVElGSUNBQ0FPLlRBQl9FTVBSRUdBRE9TIFdIRVJFIElEX0VNUCA9ICZJRDs=[/et_pb_dmb_code_snippet]

…quando eu pressionar enter, o SQL*Plus irá pedir para que eu forneça o valor para a variável “ID”, através da seguinte mensagem:

[et_pb_dmb_code_snippet _builder_version=”4.0.6″ code=”RW50ZXIgdmFsdWUgZm9yIGlkOiAyMDAwMDAKIG9sZCAxOiBTRUxFQ1QgSURfRU1QLCBOT01FX0VNUCwgU0FMQVJJTywgCiAgICAgICAgRlJPTSBDRVJUSUZJQ0FDQU8uVEFCX0VNUFJFR0FET1MgV0hFUkUgSURfRU1QID0gJklEOwogbmV3IDE6IFNFTEVDVCBJRF9FTVAsIE5PTUVfRU1QLCBTQUxBUklPLCAKICAgICAgICBGUk9NIENFUlRJRklDQUNBTy5UQUJfRU1QUkVHQURPUyBXSEVSRSBJRF9FTVAgPSAyMDAwMDA7″ hover_enabled=”0″]RW50ZXIgdmFsdWUgZm9yIGlkOiAyMDAwMDAKIG9sZCAxOiBTRUxFQ1QgSURfRU1QLCBOT01FX0VNUCwgU0FMQVJJTywgCiAgICAgICAgRlJPTSBDRVJUSUZJQ0FDQU8uVEFCX0VNUFJFR0FET1MgV0hFUkUgSURfRU1QID0gJklEOwogbmV3IDE6IFNFTEVDVCBJRF9FTVAsIE5PTUVfRU1QLCBTQUxBUklPLCAKICAgICAgICBGUk9NIENFUlRJRklDQUNBTy5UQUJfRU1QUkVHQURPUyBXSEVSRSBJRF9FTVAgPSAyMDAwMDA7[/et_pb_dmb_code_snippet]

Eu também posso fornecer dinamicamente as colunas que eu desejo buscar, inserindo essas “variáveis” na lista de colunas do SELECT:

[et_pb_dmb_code_snippet _builder_version=”4.0.6″ code=”IFNRTD4gU0VMRUNUIElEX0VNUCwgJmNvbHVuYTIsICZjb2x1bmEzLCBGUk9NIENFUlRJRklDQUNBTy5UQUJfRU1QUkVHQURPUyBXSEVSRSBJRF9FTVAgPSAmSUQ7CgogRW50ZXIgdmFsdWUgZm9yIGNvbHVuYTI6IG5vbWVfZW1wCiBFbnRlciB2YWx1ZSBmb3IgY29sdW5hMzogaWRhZGUKIEVudGVyIHZhbHVlIGZvciBpZDogMjEwMDAwCiBvbGQgMTogU0VMRUNUIElEX0VNUCwgJmNvbHVuYTIsICZjb2x1bmEzLCAKICAgICAgICBGUk9NIENFUlRJRklDQUNBTy5UQUJfRU1QUkVHQURPUyBXSEVSRSBJRF9FTVAgPSAmSUQ7CiBuZXcgMTogU0VMRUNUIElEX0VNUCwgTk9NRV9FTVAsIElEQURFLCAKICAgICAgICBGUk9NIENFUlRJRklDQUNBTy5UQUJfRU1QUkVHQURPUyBXSEVSRSBJRF9FTVAgPSAyMTAwMDA7″ hover_enabled=”0″]IFNRTD4gU0VMRUNUIElEX0VNUCwgJmNvbHVuYTIsICZjb2x1bmEzLCBGUk9NIENFUlRJRklDQUNBTy5UQUJfRU1QUkVHQURPUyBXSEVSRSBJRF9FTVAgPSAmSUQ7CgogRW50ZXIgdmFsdWUgZm9yIGNvbHVuYTI6IG5vbWVfZW1wCiBFbnRlciB2YWx1ZSBmb3IgY29sdW5hMzogaWRhZGUKIEVudGVyIHZhbHVlIGZvciBpZDogMjEwMDAwCiBvbGQgMTogU0VMRUNUIElEX0VNUCwgJmNvbHVuYTIsICZjb2x1bmEzLCAKICAgICAgICBGUk9NIENFUlRJRklDQUNBTy5UQUJfRU1QUkVHQURPUyBXSEVSRSBJRF9FTVAgPSAmSUQ7CiBuZXcgMTogU0VMRUNUIElEX0VNUCwgTk9NRV9FTVAsIElEQURFLCAKICAgICAgICBGUk9NIENFUlRJRklDQUNBTy5UQUJfRU1QUkVHQURPUyBXSEVSRSBJRF9FTVAgPSAyMTAwMDA7[/et_pb_dmb_code_snippet]

Também é possível definir em tempo de execução, o nome da tabela a ser buscada:

[et_pb_dmb_code_snippet _builder_version=”4.0.6″ code=”IFNRTD4gU0VMRUNUICogRlJPTSAmdGFiZWxhOwogRW50ZXIgdmFsdWUgZm9yIHRhYmVsYTogQ0VSVElGSUNBQ0FPLlRBQl9FTVBSRUdBRE9TCiBvbGQgMTogU0VMRUNUICogRlJPTSAmdGFiZWxhOwogbmV3IDE6IFNFTEVDVCAqIEZST00gQ0VSVElGSUNBQ0FPLlRBQl9FTVBSRUdBRE9TOw==” hover_enabled=”0″]IFNRTD4gU0VMRUNUICogRlJPTSAmdGFiZWxhOwogRW50ZXIgdmFsdWUgZm9yIHRhYmVsYTogQ0VSVElGSUNBQ0FPLlRBQl9FTVBSRUdBRE9TCiBvbGQgMTogU0VMRUNUICogRlJPTSAmdGFiZWxhOwogbmV3IDE6IFNFTEVDVCAqIEZST00gQ0VSVElGSUNBQ0FPLlRBQl9FTVBSRUdBRE9TOw==[/et_pb_dmb_code_snippet]

Basicamente é possível usar essas variáveis temporárias em todas as partes de uma query. Inclusive da ordenação:

[et_pb_dmb_code_snippet _builder_version=”4.0.6″ code=”IFNRTD4gU0VMRUNUIElEX0VNUCwgTk9NRV9FTVAgCiAgICAgIEZST00gQ0VSVElGSUNBQ0FPLlRBQl9FTVBSRUdBRE9TIAogICAgICBPUkRFUiBCWSAmY29sdW5hX29yZGVuYXIgJnRpcG9fb3JkZW5hY2FvOwogRW50ZXIgdmFsdWUgZm9yICZjb2x1bmFfb3JkZW5hcjogU0FMQVJJTwogRW50ZXIgdmFsdWUgZm9yICZ0aXBvX29yZGVuYWNhbzogREVTQwogb2xkIDE6IFNFTEVDVCBJRF9FTVAsIE5PTUVfRU1QIAogICAgICAgIEZST00gQ0VSVElGSUNBQ0FPLlRBQl9FTVBSRUdBRE9TIAogICAgICAgIE9SREVSIEJZICZjb2x1bmFfb3JkZW5hciAmdGlwb19vcmRlbmFjYW87CiBuZXcgMTogU0VMRUNUIElEX0VNUCwgTk9NRV9FTVAgCiAgICAgICAgRlJPTSBDRVJUSUZJQ0FDQU8uVEFCX0VNUFJFR0FET1MgT1JERVIgQlkgU0FMQVJJTyBERVNDOw==” hover_enabled=”0″]IFNRTD4gU0VMRUNUIElEX0VNUCwgTk9NRV9FTVAgCiAgICAgIEZST00gQ0VSVElGSUNBQ0FPLlRBQl9FTVBSRUdBRE9TIAogICAgICBPUkRFUiBCWSAmY29sdW5hX29yZGVuYXIgJnRpcG9fb3JkZW5hY2FvOwogRW50ZXIgdmFsdWUgZm9yICZjb2x1bmFfb3JkZW5hcjogU0FMQVJJTwogRW50ZXIgdmFsdWUgZm9yICZ0aXBvX29yZGVuYWNhbzogREVTQwogb2xkIDE6IFNFTEVDVCBJRF9FTVAsIE5PTUVfRU1QIAogICAgICAgIEZST00gQ0VSVElGSUNBQ0FPLlRBQl9FTVBSRUdBRE9TIAogICAgICAgIE9SREVSIEJZICZjb2x1bmFfb3JkZW5hciAmdGlwb19vcmRlbmFjYW87CiBuZXcgMTogU0VMRUNUIElEX0VNUCwgTk9NRV9FTVAgCiAgICAgICAgRlJPTSBDRVJUSUZJQ0FDQU8uVEFCX0VNUFJFR0FET1MgT1JERVIgQlkgU0FMQVJJTyBERVNDOw==[/et_pb_dmb_code_snippet]

Só devemos ter cuidado, no momento de inserir um varchar, e este contenha o caractere “&”. Caso eu não use um escape, esse caractere será interpretado pelo SQL*Plus como uma variavel a ser definida. Ex:

[et_pb_dmb_code_snippet _builder_version=”4.0.6″ code=”IFNRTD4gSU5TRVJUIElOVE8gQ0VSVElGSUNBQ0FPLlRBQl9FTVBSRUdBRE9TIAogICAgICBWQUxVRVMgKDMxMDAwMCwgJ1Rlc3RlICYgVGVzdGUnLCA1MCwgU1lTREFURSwgNiwxKTsKIEVudGVyIHZhbHVlIGZvciB0ZXN0ZTogb3BzCiBvbGQgMTogSU5TRVJUIElOVE8gQ0VSVElGSUNBQ0FPLlRBQl9FTVBSRUdBRE9TIAogICAgICAgIFZBTFVFUyAoMzEwMDAwLCAnVGVzdGUgJiBUZXN0ZScsIDUwLCBTWVNEQVRFLCA2LDEpOwogbmV3IDE6IElOU0VSVCBJTlRPIENFUlRJRklDQUNBTy5UQUJfRU1QUkVHQURPUyAKICAgICAgICBWQUxVRVMgKDMxMDAwMCwgJ1Rlc3RlIG9wcycsIDUwLCBTWVNEQVRFLCA2LDEpOw==” hover_enabled=”0″]IFNRTD4gSU5TRVJUIElOVE8gQ0VSVElGSUNBQ0FPLlRBQl9FTVBSRUdBRE9TIAogICAgICBWQUxVRVMgKDMxMDAwMCwgJ1Rlc3RlICYgVGVzdGUnLCA1MCwgU1lTREFURSwgNiwxKTsKIEVudGVyIHZhbHVlIGZvciB0ZXN0ZTogb3BzCiBvbGQgMTogSU5TRVJUIElOVE8gQ0VSVElGSUNBQ0FPLlRBQl9FTVBSRUdBRE9TIAogICAgICAgIFZBTFVFUyAoMzEwMDAwLCAnVGVzdGUgJiBUZXN0ZScsIDUwLCBTWVNEQVRFLCA2LDEpOwogbmV3IDE6IElOU0VSVCBJTlRPIENFUlRJRklDQUNBTy5UQUJfRU1QUkVHQURPUyAKICAgICAgICBWQUxVRVMgKDMxMDAwMCwgJ1Rlc3RlIG9wcycsIDUwLCBTWVNEQVRFLCA2LDEpOw==[/et_pb_dmb_code_snippet]

Para evitarmos isso, usamos um escape, que eu prefiro chamar de “Gambiarra da Oracle”:

[et_pb_dmb_code_snippet _builder_version=”4.0.6″ code=”IFNRTD4gSU5TRVJUIElOVE8gQ0VSVElGSUNBQ0FPLlRBQl9FTVBSRUdBRE9TIAogICAgICBWQUxVRVMgKDMxMDAwMCwgJ1Rlc3RlICYnIHx8ICcgVGVzdGUnLCA1MCwgU1lTREFURSwgNiwxKTsKCiAxIHJvdyBjcmVhdGVkLgrigKYgb3UgcG9kZW1vcyBkZXNhdGl2YXIgbyBwcm9tcHQgZGUgdmFyacOhdmVpcyBubyBTUUwqUGx1cywgYXNzaW0sIG8gY2FyYWN0ZXJlIOKAnCbigJ0gYW50ZXMgZGUgcXVhbHF1ZXIgcGFsYXZyYSBzZXLDoSBpbnRlcnByZXRhZG8gY29tbyB1bWEgc3RyaW5nLiBFeDoKCiBTUUw+IFNFVCBERUZJTkUgT0ZGCiBTUUw+IElOU0VSVCBJTlRPIENFUlRJRklDQUNBTy5UQUJfRU1QUkVHQURPUyAKICAgICAgVkFMVUVTICgzMTAwMDAsICdUZXN0ZSAmIFRlc3RlJywgNTAsIFNZU0RBVEUsIDYsMSk7CgogMSByb3cgY3JlYXRlZC4=” hover_enabled=”0″]IFNRTD4gSU5TRVJUIElOVE8gQ0VSVElGSUNBQ0FPLlRBQl9FTVBSRUdBRE9TIAogICAgICBWQUxVRVMgKDMxMDAwMCwgJ1Rlc3RlICYnIHx8ICcgVGVzdGUnLCA1MCwgU1lTREFURSwgNiwxKTsKCiAxIHJvdyBjcmVhdGVkLgrigKYgb3UgcG9kZW1vcyBkZXNhdGl2YXIgbyBwcm9tcHQgZGUgdmFyacOhdmVpcyBubyBTUUwqUGx1cywgYXNzaW0sIG8gY2FyYWN0ZXJlIOKAnCbigJ0gYW50ZXMgZGUgcXVhbHF1ZXIgcGFsYXZyYSBzZXLDoSBpbnRlcnByZXRhZG8gY29tbyB1bWEgc3RyaW5nLiBFeDoKCiBTUUw+IFNFVCBERUZJTkUgT0ZGCiBTUUw+IElOU0VSVCBJTlRPIENFUlRJRklDQUNBTy5UQUJfRU1QUkVHQURPUyAKICAgICAgVkFMVUVTICgzMTAwMDAsICdUZXN0ZSAmIFRlc3RlJywgNTAsIFNZU0RBVEUsIDYsMSk7CgogMSByb3cgY3JlYXRlZC4=[/et_pb_dmb_code_snippet]

Também é possível definir uma variável no SQL*Plus, mas para isso, é necessário reverter o passo anterior:

[et_pb_dmb_code_snippet _builder_version=”4.0.6″ code=”IFNRTD4gU0VUIERFRklORSBPTgogU1FMPiBERUZJTkUgdGFiZWxhID0gQ0VSVElGSUNBQ0FPLlRBQl9FTVBSRUdBRE9TCiBTUUw+IFNFTEVDVCAqIEZST00gJnRhYmVsYTsKIG9sZCAxOiBTRUxFQ1QgKiBGUk9NICZ0YWJlbGE7CiBuZXcgMTogU0VMRUNUICogRlJPTSBDRVJUSUZJQ0FDQU8uVEFCX0VNUFJFR0FET1M7″ hover_enabled=”0″]IFNRTD4gU0VUIERFRklORSBPTgogU1FMPiBERUZJTkUgdGFiZWxhID0gQ0VSVElGSUNBQ0FPLlRBQl9FTVBSRUdBRE9TCiBTUUw+IFNFTEVDVCAqIEZST00gJnRhYmVsYTsKIG9sZCAxOiBTRUxFQ1QgKiBGUk9NICZ0YWJlbGE7CiBuZXcgMTogU0VMRUNUICogRlJPTSBDRVJUSUZJQ0FDQU8uVEFCX0VNUFJFR0FET1M7[/et_pb_dmb_code_snippet]

Por último, mas não menos importante, é possível ativar o caractere de escape, ex:

[et_pb_dmb_code_snippet _builder_version=”4.0.6″ code=”IFNRTD4gU0VUIEVTQ0FQRSBPTgogU1FMPiBJTlNFUlQgSU5UTyBDRVJUSUZJQ0FDQU8uVEFCX0VNUFJFR0FET1MgVkFMVUVTICgzMTAwMDAsICdUZXN0ZSAmIFRlc3RlJywgNTAsIFNZU0RBVEUsIDYsMSk7CgogMSByb3cgY3JlYXRlZC4=” hover_enabled=”0″]IFNRTD4gU0VUIEVTQ0FQRSBPTgogU1FMPiBJTlNFUlQgSU5UTyBDRVJUSUZJQ0FDQU8uVEFCX0VNUFJFR0FET1MgVkFMVUVTICgzMTAwMDAsICdUZXN0ZSAmIFRlc3RlJywgNTAsIFNZU0RBVEUsIDYsMSk7CgogMSByb3cgY3JlYXRlZC4=[/et_pb_dmb_code_snippet]

É isso ai pessoal. Até o próximo post.