使用sqlcmd.exe运行sql脚本

时间:2014-09-16 16:14:35

标签: sql-server sql-server-2008 sqlcmd

我有一个应该执行的.sql文件,这个命令如下:

set auth=-S 192.168.1.200 -d solarix -U sa -P masterkey  -f 65001
set sqlcmd="%ProgramFiles%\Microsoft SQL Server\100\Tools\Binn\sqlcmd.exe"
%sqlcmd% -i dictionary.sql -b -a 32767 -o mssql-dictionary.log %auth%

我怎样才能让它发挥作用?

2 个答案:

答案 0 :(得分:0)

不完全确定您要完成的任务。

您可以打开PowerShell或cmd,并在参数中键入以下内容。

sqlcmd -S <ComputerName>\<InstanceName> -i <MyScript.sql>

来自:http://msdn.microsoft.com/en-us/library/ms180944.aspx

答案 1 :(得分:0)

所以我跑了这个:

set _auth=-S . -d Master -U sa -P mypassword  -f 65001
set _sqlcmd="%ProgramFiles%\Microsoft SQL Server\110\Tools\Binn\SQLCMD.EXE"
%_sqlcmd%  -i dictionary.sql -b -a 32767 -o mssql-dictionary.log  %_auth%
set _auth=
set _sqlcmd=

我的dictionary.sql文件只包含一行:

Select @@Version

(注意我选择“Master”作为我的数据库)。

注意我的服务器-S参数是“。”。

以上工作。

当我输入“。”的IP地址时。机。

set _auth=-S 192.168.1.101 -d Master -U sa -P mypassword -f 65001
set _sqlcmd="%ProgramFiles%\Microsoft SQL Server\110\Tools\Binn\SQLCMD.EXE"
%_sqlcmd%  -i dictionary.sql -b -a 32767 -o mssql-dictionary.log %_auth%

set _auth=
set _sqlcmd=

当我输入IP地址时,它无效。

这意味着即使我在“192.168.1.101”,也没有为远程连接设置Sql Server。

这是穷人测试连接的方法。

转到Control-Panel / Admin-Tools / ODBC

创建一个系统dsn(临时的,你可以稍后删除)....输入你的凭据......看看你是否可以连接。

您的sqlcmd参数可能正确,但您不能远程连接到该计算机。

但是代码的语法糖看起来是正确的。

您会看到类似这样的内容:

Sqlcmd: Error: Microsoft SQL Server Native Client 11.0 : Named Pipes Provider: Could not open a connection to SQL Server [5]. .
Sqlcmd: Error: Microsoft SQL Server Native Client 11.0 : Login timeout expired.
Sqlcmd: Error: Microsoft SQL Server Native Client 11.0 : A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online..

在日志文件中,如果无法连接到sql-server。