I write a script for sqlplus . I'm trying to output the results to a file. I do it like this:

 spool D:\folder\scripts.log 

In this case, if the specified directory does not exist, the file is not recorded.

Question 1: Can I somehow make sqlplus create the necessary directories?

When the append command is added append is added to existing files. But if there is no file, then it is not created.

 spool D:\folder\scripts.log append 

Question 2: Can I somehow create a file, and if it has already been created, then add it to it?

  • Question 1 - host MD <dir> . Question 2 - the file is created with append , as I recall. - 0xdb
  • 1 - I tried to just add a script file. Something went wrong. 2 - with the appendix something strange. The file is created, but the next time you start writing to the file does not go. spool D:\folder\scripts.log append did so. - Viktorov
  • one
    Shorten the script file to the example being played and add it to the question. - 0xdb
  • When playing on the minimum example figured out. I just didn’t have enough rights, what was the corresponding entry in the console, I just missed it. Please reply by accepting an answer. - Viktorov
  • Well, only in the evening I can. - 0xdb

1 answer 1

Short answer:

  1. You can use the host command directive to create pathnames.
  2. The file will be created with the append option, even if it does not already exist.

So that the answer to both questions would look convincing, I sketched a short script:

 set trimspool on define off head off host cmd /c dir /b logs spool logs/test1.log host cmd /c if not exist logs md logs spool logs/test1.log append set termout off select '1 test output' output from dual; spool logs/test2.log select '2 test output' output from dual; spool logs/test1.log append select '3 test output' output from dual; spool off host cmd /c dir /b logs\*.log & type logs\*.log exit 

And I added its output with comments:

 --папка ещё не существует File Not Found -- и лог файл не создался SP2-0606: Cannot create SPOOL file "logs/test1.log" -- оба лог файлика поочерёдно создались, причём первый с append test1.log test2.log logs\test1.log 1 test output 3 test output logs\test2.log 2 test output 

To the note: In response to a question that is not directly related to the current one, I gave an example of the dynamic definition of the sql script to run. Similarly, all parameters for spool , host , etc., can be dynamically determined.