Redirect the output of a query in a file

18,285

You are mixing shell and sqlplus commands. sqlplus doesn't understand redirections, you have to use the spool keyword.

For instance:

SQL> spool /tmp/result.txt
SQL> start requete.sql
SQL> spool off

From shell:

$ sqlplus scott/tiger @requete.sql > /tmp/result.txt
Share:
18,285

Related videos on Youtube

Aomine Daiki
Author by

Aomine Daiki

Updated on September 18, 2022

Comments

  • Aomine Daiki
    Aomine Daiki over 1 year

    What can I do to redirect the query result in a text file in sqlplus

    I tried: start requete.sql > resultat.txt but it doesn't work.

  • apaul
    apaul about 9 years
    I've written /tmp/result.txt but the path is up to you. Just replace /tmp/result.txt by /path/to/the/file/you/want.txt. It's the same for the start or @ keywords: @/path/to/you/file.sql
  • Aomine Daiki
    Aomine Daiki about 9 years
    i have a problem spool dosen't work
  • apaul
    apaul about 9 years
    Impossible. Even if you specfied a path whose access is denied, you'd get an error. Note that when you use spool and specify no more options, the result of query is also displayed on your screen. You'll have to give more info than just "doesn't work".
  • Aomine Daiki
    Aomine Daiki about 9 years
    this is the error: SP2-0606: Cannot create SPOOL file "/resultat.txt"
  • apaul
    apaul about 9 years
    It cannot create the file because you try to write in the root (/). At this place, only the user root is allowed to create files and directories. Specify a path where your user has enough rights (in his home directory for instance)