BCP in SQL command give NativeError = 2 when no -S parameter on local DB
sql, sql-server, sql-server-2008
Solution
If you ommit -S then it will try the default, localhost unnamed instance:
`-S server_name[ \instance_name]` Specifies the instance of SQL Server to which to connect. If no server is specified, the bcp utility connects to the default instance of SQL Server on the local computer.
From your example it seems you do have an instance name, so it will not be possible to connect w/o specifying the -S explicitly. Besides, your code will always be more portable and easier to troubleshoot if you take the extra steps of specifying the -S params explicitly. Use `SERVERPROPERTY(MachineName)` and `SERVERPROPERTY(InstanceName)`, make sure the code is cluster aware.
Problem
I have this command in 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 ``` and it outputs this: ``` 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` is 1 It works only if I add -S parameter with server name and database name (like this `-S "MYCOMPUTERNAME\DBINSTANCENAME"`). But when I try this on remote server this works without `-S` parameter. How can I setup my local DB, so I don't need `-S` anymore?