Frederico Almeida
Frederico Almeida

Reputation: 317

sqlcmd error when -S Server

I have a quite weird situation. I'm using sqlcmd to run a script with the command:

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

SName is the IP\Instance like 10.10.10.100\MyInstance

The content of test.sql is simply:

select @@servername
select getdate()

When I run it gives me the following error:

Sqlcmd: Error: Microsoft ODBC Driver 11 for SQL Server : Data source name not found and no default driver specified.

***If I copy and paste the script into another machine and run it works fine.

Why is it trying to connect via ODBC?

I believe it's an environment issue but I don't know where to start looking.

Thanks

Upvotes: 3

Views: 4672

Answers (1)

Chris Pickford
Chris Pickford

Reputation: 8991

When using SQLCMD from the command line, it uses the Microsoft ODBC driver by default.

From 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.

You'll need to configure ODBC in the target environment for SQLCMD to work.

Upvotes: 3

Related Questions