Getting connectivity error when using :connect command in sqlcmd, but not when doing command line connect

StackOverflow https://stackoverflow.com/questions/1917124

  •  20-09-2019
  •  | 
  •  

Question

I'm just starting out with sqlcmd on SQL Server 2005, enterprise edition.

I can connect to my server fine, from the command line:

sqlcmd -SSQLSERVERNAME -Q"select test=1"

However, when I create a junk.sql file that has just this one line:

:connect -SSQLSERVERNAME

...and try to run it by running the following at the command line:

sqlcmd -i C:\junk\junk.sql

...I can't even connect - I get this error:

Named Pipes Provider: Could not open a connection to SQL Server [2]. Sqlcmd: Error: Microsoft SQL Native Client : An error has occurred while establishing a connection to the server. When c onnecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections.. Sqlcmd: Error: Microsoft SQL Native Client : Login timeout expired.

From what I read, I thought that using the connect command should work. Am I just using it incorrectly? What I'd like to do is connect in the sql file using the connect command (eventually using variables for different servers) and then run different sql files using the :r command

Thoughts?

thanks, Sylvia

Was it helpful?

Solution

The :connect command does not need the -S switch, just use

:connect "SQLSERVERNAME"

OTHER TIPS

Okay - I was doing 2 things wrong:

** It appears that when you run sqlcmd, you must be connecting to something from the get-go. I thought I could just do this:

sqlcmd -i C:\junk\junk.sql

but I had to do this

sqlcmd -i -SServerName C:\junk\junk.sql

I didn't have a local default instance running - presumably if you have a local default instance running, there's not a problem.

** Then in the junk.sql, you can do the following

:connect ServerName

note that it's NOT

:connect -SServerName

Hope this helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top