6

I do some command line batch (.bat) with sqlcmd as this way:

sqlcmd -i Scripts\STEP01.sql -o PROCESS.log -S MYSERVER -E -d MYDATABASE

and i need an output file (it's works currently) and also the output trought the screen to do something like:

@echo off
echo The result of the query was:
    sqlcmd -i Scripts\STEP01.sql -o PROCESS.log -S MYSERVER -E -d MYDATABASE
pause
CHOICE /C:YN /M "Is the result accord?"
IF ERRORLEVEL 2 GOTO ENDWITHERROR
IF ERRORLEVEL 1 GOTO STEP2

Note:

  • Yes, the script works suscefull, it's not an issue question.
  • And yes, I've a "print "something" on my sql. The log file gets the output.

Thanks a lot!

Similar question without answer: How to run a sql script file using sqlcmd and output to both shell and file

Community
  • 1
  • 1
Leandro Bardelli
  • 10,561
  • 15
  • 79
  • 116
  • 2
    If the log isn't too big, you could always add a TYPE PROCESS.LOG to the BAT file before the Pause. Don't know if this will help and don't know of any way to get both file and screen output... – Sparky Feb 16 '12 at 23:35
  • watch out for buffering http://stackoverflow.com/q/306945/10245 – Tim Abell May 28 '15 at 11:27

2 Answers2

2

I also couldn't find a better way then @Sparky proposed. The following code adds his suggestion:

@echo off

:: this will execute the script into PROCESS.log
sqlcmd -i Scripts\STEP01.sql -o PROCESS.log -S MYSERVER -E -d MYDATABASE

:: this present the contents of PROCESS.log to the screen
echo The result of the query was:
type PROCESS.log

pause
CHOICE /C:YN /M "Is the result accord?"
IF ERRORLEVEL 2 GOTO ENDWITHERROR
IF ERRORLEVEL 1 GOTO STEP2
Gerardo Lima
  • 6,467
  • 3
  • 31
  • 47
  • 1
    You can get more information about command redirection at this links [link](http://msdn.microsoft.com/en-us/library/ie/cc772622(v=ws.10).aspx), [link](http://ss64.com/nt/syntax-redirection.html). – Gerardo Lima Apr 24 '12 at 18:14
2

If you want you can use powershell instead and use the following:

sqlcmd -i Scripts\STEP01.sql  -S SERVER -E -d MYDB | Tee-Object -file "PROCESS.log"

More info from Microsoft

jgerstle
  • 1,674
  • 5
  • 25
  • 35