Write Oracle SQL File

Asked

Viewed 191 times

0

Good morning,

I am beginner in Oracle PL/SQL, and I created the script below to record the result of a query in txt file, runs without errors but does not write the file, anyone knows what could be happening ? Where am I going wrong ?

DECLARE arquivo_saida
UTL_File.File_Type; Cursor Cur_Linha is   Select OWNER||'  '||TABLE_NAME as Linha From All_Tables Where Owner Like'SYS%';
BEGIN arquivo_saida := UTL_File.Fopen('C:\teste','arquvo.txt','w');   
For Reg_Linha in Cur_linha Loop
UTL_File.Put_Line(arquivo_saida, Reg_linha.linha);
End Loop; 
UTL_File.Fclose(arquivo_saida);   
Dbms_Output.Put_Line('Arquivo gerado com sucesso.');
EXCEPTION
WHEN UTL_FILE.INVALID_OPERATION THEN
Dbms_Output.Put_Line('Operação inválida no arquivo.'); 
UTL_File.Fclose(arquivo_saida);
WHEN UTL_FILE.WRITE_ERROR THEN
Dbms_Output.Put_Line('Erro de gravação no arquivo.');
UTL_File.Fclose(arquivo_saida);
WHEN UTL_FILE.INVALID_PATH THEN
Dbms_Output.Put_Line('Diretório inválido.');
UTL_File.Fclose(arquivo_saida);
WHEN UTL_FILE.INVALID_MODE THEN
Dbms_Output.Put_Line('Modo de acesso inválido.');
UTL_File.Fclose(arquivo_saida); 
WHEN Others THEN
Dbms_Output.Put_Line('Problemas na geração do arquivo.');
UTL_File.Fclose(arquivo_saida);
END;
  • 1

    Have you set the UTL or Diretory folder on Oracle ? https://www.devmedia.com.br/pacote-utl_file-do-oracle/13129 https://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_5007.htm

  • Does it not have to be 'C: test' than 'C: test'?

  • @Motta his link was super useful, only needed to set the directory before using: CREATE DIRECTORY DIRECTORIES AS 'C: DIRECTORIES';

1 answer

0


As I commented in the @Motta reply, the creation of the output directory was missing with the command below:

CREATE DIRECTORY DIRETORIO AS 'C:\DIRETORIO'; 

Then replace the path with the variable:

BEGIN arquivo_saida := UTL_File.Fopen('DIRETORIO','arquvo.txt','w');   

Browser other questions tagged

You are not signed in. Login or sign up in order to post.