我在SQL中有这个命令:
DECLARE @cmd NVARCHAR(4000)
DECLARE @pathAndFileName NVARCHAR(MAX)
DECLARE @result INT
SET @pathAndFileName = 'C:\Temp\file.csv'
SET @cmd = 'bcp "SELECT TOP 1 * FROM SomeDB.dbo.SomeTable" queryout "'
+ @pathAndFileName + '" -w -T -t; '
EXEC @result = xp_cmdshell @cmd
SELECT @result
并输出:
SQLState = 08001, NativeError = 2 Error = [Microsoft][SQL Server Native Client 11.0]Named Pipes Provider: Could not open a connection to SQL Server [2].
SQLState = 08001, NativeError = 2 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.
SQLState = S1T00, NativeError = 0 Error = [Microsoft][SQL Server Native Client 11.0]Login timeout expired
@result
是1
只有在我添加带服务器名称和数据库名称的-S参数时才有效(如此-S "MYCOMPUTERNAME\DBINSTANCENAME"
)。
但是当我在远程服务器上尝试此操作时,这可以不使用-S
参数。
如何设置我的本地数据库,所以我不再需要-S
了?
答案 0 :(得分:9)
如果您省略-S,则会尝试默认值localhost unnamed instance:
-S server_name[ \instance_name]
指定要连接的SQL Server实例。 如果未指定服务器,则bcp实用程序将连接到本地计算机上的默认SQL Server实例。
从您的示例中看来您确实有一个实例名称,因此无法连接w / o显式指定-S。此外,如果您采取额外的步骤明确指定-S参数,您的代码将始终更便携,更容易进行故障排除。使用SERVERPROPERTY(MachineName)
和SERVERPROPERTY(InstanceName)
,确保代码可以识别群集。