-S 服务器时出现 sqlcmd 错误

sqlcmd error when -S Server

我的情况很奇怪。我正在使用 sqlcmd 运行 一个带有命令的脚本:

sqlcmd -S %SName% -U %UName% -P %Pwd% -d %DbName% -I -i test.sql >> test_log.txt 2>&1

SName 是 IP\Instance 就像 10.10.10.100\MyInstance

test.sql的内容就是:

select @@servername
select getdate()

当我 运行 它给我以下错误:

Sqlcmd:错误:SQL 服务器的 Microsoft ODBC 驱动程序 11:未找到数据源名称且未指定默认驱动程序。

***如果我将脚本复制并粘贴到另一台机器上并且 运行 它工作正常。

为什么要尝试通过 ODBC 连接?

我认为这是一个环境问题,但我不知道从哪里开始寻找。

谢谢

从命令行使用 SQLCMD 时,默认使用 Microsoft ODBC 驱动程序。

来自https://msdn.microsoft.com/en-us/library/ms162773.aspx

SQL Server Management Studio uses the Microsoft .NET Framework SqlClient for execution in regular and SQLCMD mode in Query Editor. When sqlcmd is run from the command line, sqlcmd uses the ODBC driver. Because different default options may apply, you might see different behavior when you execute the same query in SQL Server Management Studio in SQLCMD Mode and in the sqlcmd utility.

您需要在目标环境中配置 ODBC,SQLCMD 才能正常工作。