0

On Windows using SQLCMD, we have at least 2 ways to output logs to some logfile. Either we can use option -o with specified file name, or we can redirect output via console redirect >> to append to a new file. I prefer using the 2nd option.

sqlcmd -d myDb -P pass -U user -H myHost -i F:\sqlScript.sql -o F:\log.txt
sqlcmd -d myDb -P pass -U user -H myHost -i F:\sqlScript.sql >> F:\log.txt

From my experience, whenever I run longer queries, I don't receive output to the log file immediately, but right after the query is completed. Is there a way to force output to the file immediately?

(I feel that this might be related to both sql query/session settings and the sqlcmd/cmd settings.)


This question can be considered as a follow up on SQLCMD command, How to save output into log file .

Tatranskymedved
  • 4,194
  • 3
  • 21
  • 47
  • How do you know that output to the file is done until the query is completed? Do you check file size at regular intervals via a process parallel to the `sqlcmd` command? What happen when the output is _not_ redirected to the file? If it appears on the screen before the query is completed, then your conclusion is wrong... – Aacini Jul 04 '19 at 08:00
  • I don't know how `sqlcmd` behaves, but `>>` opens the file once and closes it when the redirected command is done, so reading from an open file could be tricky, and Windows Explorer might not update the file size unless you force it to by _Ctrl+F5_. The `-o` option completely depends on `sqlcmd`, of course... – aschipfl Jul 04 '19 at 16:31

0 Answers0