sqlcmd with output file and screen output

橙三吉。 提交于 2019-12-12 11:13:56

问题


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


回答1:


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



回答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



来源:https://stackoverflow.com/questions/9319025/sqlcmd-with-output-file-and-screen-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!