Exemplo de programa para tratamento de arquivo utl_file Oracle
Autor: André Borges | Publicado: 2011-01-18T16:46:00-02:00

&nbsp;&nbsp; &nbsp;Segue exemplo de programa para tratamento de arquivos utilizando o pacote do Oracle utl_file.
&nbsp;&nbsp; &nbsp;Neste programa, considero que as configurações de diretório Oracle e permissões dos diretórios no servidor já estejam parametrizadas.



create or replace procedure exemplo_utl_file_grava is
l_arq &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;utl_file.file_type;
l_nome_arq &nbsp; &nbsp; &nbsp; varchar2(250);
l_linha &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;varchar2(250);
l_conta &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;varchar2(250) := 'inicio';
l_conta_ant &nbsp; &nbsp; &nbsp;varchar2(250) := 'fim';
&nbsp;&nbsp; &nbsp; procedure p_abre_arquivo(l_arq in out utl_file.file_type,
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;l_nome_arq in varchar2) is
&nbsp;&nbsp; &nbsp; begin
&nbsp;&nbsp; &nbsp; &nbsp; l_arq := null;
&nbsp;&nbsp; &nbsp; &nbsp; l_arq := utl_file.fopen('INTERFACE',l_nome_arq,'W');
&nbsp;&nbsp; &nbsp; exception
&nbsp;&nbsp; &nbsp; when others then
&nbsp;&nbsp; &nbsp; &nbsp; dbms_output.put_line('Erro ao abrir o arquivo');
&nbsp;&nbsp; &nbsp; end p_abre_arquivo;


&nbsp;&nbsp; &nbsp; procedure p_grava_linha(l_arq in out utl_file.file_type,l_linha varchar2) is
&nbsp;&nbsp; &nbsp; begin
&nbsp;&nbsp; &nbsp; &nbsp; if length(l_linha) = 240 then
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; utl_file.put_line(l_arq,l_linha);
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; utl_file.fflush(l_arq);
&nbsp;&nbsp; &nbsp; &nbsp; else
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; dbms_output.put_line('Erro linha com tamanho inválido');
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; dbms_output.put_line(l_linha);
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; raise_application_error(20001, 'Erro linha com tamanho inválido');
&nbsp;&nbsp; &nbsp; &nbsp; end if;
&nbsp;&nbsp; &nbsp; end p_grava_linha;


&nbsp;&nbsp; &nbsp; procedure p_fecha_arquivo(l_arq in out utl_file.file_type) is
&nbsp;&nbsp; &nbsp; begin
&nbsp;&nbsp; &nbsp; &nbsp; utl_file.fclose(l_arq);
&nbsp;&nbsp; &nbsp; exception
&nbsp;&nbsp; &nbsp; when others then
&nbsp;&nbsp; &nbsp; &nbsp; dbms_output.put_line('Erro ao fechar o arquivo');
&nbsp;&nbsp; &nbsp; end p_fecha_arquivo;


begin


null;
&nbsp;&nbsp;for r in (select 'conta' nome_conta, 'banco' BAN_CODIGO&nbsp;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;from dual tab_temp
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;order by 1-- por conta
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;)loop
&nbsp;&nbsp; &nbsp; if r.nome_conta &lt;&gt; l_conta_ant then
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;if 'fim' &lt;&gt; l_conta_ant then
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-- trailler lote
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;l_linha := lpad(r.BAN_CODIGO,3,'0')|| --Codigo do banco.
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '0000'|| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --Lote de serviço
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '0'|| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;--Tipo de Registro
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lpad(' ',1,' '); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --Uso Exclusivo FEBRABAN / CNAB &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;p_grava_linha(l_arq,l_linha);
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-- fecha o arquivo
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;p_fecha_arquivo(l_arq);
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;end if;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;-- abre um novo arquivo. &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;l_conta_ant := r.nome_conta;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;p_abre_arquivo(l_arq,'ARQ_'||l_conta_ant);


&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;-- monta os cabeçalhos de arquivo e lote.
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;-- header arq
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;l_linha := lpad(r.BAN_CODIGO,3,'0')|| --Codigo do banco.
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '0000'|| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --Lote de serviço
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '0'|| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;--Tipo de Registro
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lpad(' ',9,' '); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --Uso Exclusivo FEBRABAN / CNAB &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;p_grava_linha(l_arq,l_linha);
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;
&nbsp;&nbsp; &nbsp; end if;
&nbsp;&nbsp; &nbsp; -- linha do arquivo
&nbsp;&nbsp; &nbsp; l_linha := lpad(r.BAN_CODIGO,3,'0')|| --Codigo do banco.
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '0000'|| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --Lote de serviço
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '0'|| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;--Tipo de Registro
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lpad(' ',9,' '); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --Uso Exclusivo FEBRABAN / CNAB &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp;&nbsp; &nbsp; p_grava_linha(l_arq,l_linha);
&nbsp;&nbsp; &nbsp;&nbsp;
&nbsp;&nbsp;end loop;
&nbsp;&nbsp;if 'fim' &lt;&gt; l_conta_ant then
&nbsp;&nbsp; &nbsp;-- trailler lote
&nbsp;&nbsp; &nbsp;l_linha := lpad(r.BAN_CODIGO,3,'0')|| --Codigo do banco.
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '0000'|| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --Lote de serviço
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '0'|| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;--Tipo de Registro
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lpad(' ',1,' '); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --Uso Exclusivo FEBRABAN / CNAB &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp;&nbsp; &nbsp;p_grava_linha(l_arq,l_linha);
&nbsp;&nbsp; &nbsp;-- fecha o arquivo
&nbsp;&nbsp; &nbsp;p_fecha_arquivo(l_arq);
&nbsp;&nbsp;end if;
end exemplo_utl_file_grava;
/