SQLCMD命令,如何将输出保存到日志文件 [英] SQLCMD command, How to save output into log file

查看:1043
本文介绍了SQLCMD命令,如何将输出保存到日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下问题帮助我解决了执行文件中多个SQL脚本的问题. 在目录中运行所有SQL文件 但是,我没有得到如何将输出重定向到单独的日志文件中.有人建议使用以下脚本,但由于我不理解它,因此它无法正常工作,并且无法找到错误.

The following question has helped me solving the problem of executing multiple SQL Scripts located in file. Run all SQL files in a directory However, I did not get how to redirect the output into a separate log file. Someone suggested the following script but since I don't understand it, it did not work and I can't find out the error.

for %f in (*.sql) do sqlcmd /S <servername> /d <dbname> /E /i "%f" >> sql.log 2>&1) 

推荐答案

如果需要将输出输出到一个公共文件中,则应使用@Abhishek的答案.

If you need the output into one common file then you should use the @Abhishek 's answer.

如果需要将输出输出到每个输入sql文件的单独日志文件中 那么您可以使用sqlcmd命令的-o参数.您的bat文件可能如下所示:

If you need the output into a separate log file for an each input sql file then you can use -o parameter of sqlcmd command. Your bat file could look like this:

for %%G in (*.sql) do sqlcmd /S <servername> /d <dbname> -E -i"%%G" -o C:\logs\%%G.log
pause

在这种情况下

1.sql

1.sql

2.sql

您将得到:

1.sql.log

1.sql.log

2.sql.log

2.sql.log

这篇关于SQLCMD命令,如何将输出保存到日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆