Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQLCMD: Cannot connect to remote server.

Author  Topic 

Flip81
Starting Member

1 Post

Posted - 2011-07-27 : 12:15:07
Hi all. Noob here to SQLCMD scripting. I'm trying to do a simple query using SQLCMD and I have to call the server remotely. I've verified the Username and Password but still can't connect.

Here is the script:


:setvar localserver "-U xxxx -P xxxxxxxx -S xxxxxxx"
print '$(localserver)'

:connect $(localserver)

-- set the database

:setvar mydatabase xxxxxx
PRINT '$(mydatabase)'

use $(mydatabase);
go

:setvar TestTable xxxxxx

--set top count variable
:setvar Rowcount 10

--select top number of records from Testtable based on RowCount Variable

select top $(Rowcount) * from $(TestTable)
go


Here is my error message:

Connecting to -U xxxx -P xxxxxx -S xxxxxxx...
Fatal scripting error. Cannot open connection specified in the SQLCMD script.
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

Thanks in advance!

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-07-27 : 12:37:56
Not sure if SQLCMD will parse the environment variable to pick up the username and password. This should work:

:setvar SQLCMDUSER "sqlusername"
:setvar SQLCMDPASSWORD "sqlpassword"
:setvar localserver "(local)"
:connect $(localserver)
Go to Top of Page
   

- Advertisement -