Oracle write to file

前端 未结 5 864
礼貌的吻别
礼貌的吻别 2021-01-06 03:18

I am running oracle and have a query which pulls some results from the database. I would like to write the results as a text file. How would I go about doing this?

M

相关标签:
5条回答
  • 2021-01-06 03:59

    Use UTL_FILE in combination with CREATE DIRECTORY for ease of mapping a directory path with a name (it does not create the actual directory just a reference to it so ensure it is created first)

    example

    
      create directory logfile as 'd:\logfile'; -- must have priv to do this
    
    declare
      vFile utl_file.file_type;
    begin
      vFile := utl_file.fopen(logfile ,'syslog','w'); -- w is write. This returns file handle
      utl_file.put(vFile,'Start Logfile'); -- note use of file handle vFile
      utl_file.fclose(vFile); -- note use of file handle vFile
    end;
    
    
    0 讨论(0)
  • 2021-01-06 04:02

    This seems to be a reasonable tutorial with a few simple examples UTL_FILE example

    0 讨论(0)
  • 2021-01-06 04:08

    If you are using PL/SQL then you can use the UTL_FILE package, the difference from using sql+ spool is that the files are written to the server file system. UTL_FILE has a number of limitations so an alternative on the server side would be to use Java stored procedures.

    0 讨论(0)
  • 2021-01-06 04:10

    If you're using Sql Plus, is as easy as:

    SQL> spool c:\temp\out.txt
    SQL> SELECT * FROM USERS;
    SQL> spool off
    

    This three sentences will output the result of the query "SELECT * FROM USERS" to the file c:\temp\out.txt.

    You can format this query using the string manipulation functions of Oracle.

    0 讨论(0)
  • 2021-01-06 04:10

    If you're running the query from sqlplus you can use the spool command:

    spool /tmp/test.spool
    

    After executing the spool command within a session, all output is sent to the sqlplus console as well as the /tmp/test.spool text file.

    0 讨论(0)
提交回复
热议问题