Not able to connect to the SQLCMD Utility - sql

When I try to connect to the sql server using the sqlcmd utility, I use the following syntax:
sqlcmd -q "SELECT * FROM AdventureWorks2008R2.Person.Person"
I get this following error, can anyone pleae help on this.
HResult 0x2, Level 16, State 1
Named Pipes Provider: Could not open a connection to SQL Server [2].
Sqlcmd: Error: Microsoft SQL Native Client : An error has occurred while establi
shing a connection to the server. When connecting to SQL Server 2005, this failu
re 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.

Supply the server you wish to connect to:
sqlcmd -S <server> -q "SELECT * FROM AdventureWorks2008R2.Person.Person"
For instance if you use SQL Server Express <server> would be .\SQLEXPRESS
For further options of sqlcmd use sqlcmd -?

sqlcmd -S <server> -d <database> -q "query"
this syntax worked perfectly for me...

First connect to SQL Server, as:
C:\>sqlcmd -U sa -P your_password
Then enter queries:
SELECT * FROM AdventureWorks2008R2.Person.Person;
When you want to exit out of sqlcmd, just type:
quit
and enter.

Related

SQLCMD from SSMS is not working; connecting to Azure SQL Database

Running this from my SQL Server Management Studuio (SSMS) connecting to Azure sql database. I am using SQLCMD mode of SSMS.
:Connect -S taiobdemo1.database.windows.net -d taiobcustomer -U sqlusername -P strongpw
Getting this error:
A fatal scripting error occurred. Incorrect syntax was encountered
while parsing :Connect.
The :connnect SqlCmd command does not allow you to specify a target database. You'll always connect to your default database. So it's pretty useless in Azure SQL Database, as you'll be connected to Master and won't be able to switch.
eg
:Connect xxxx.database.windows.net -U someuser -P xxxx
select db_name()
Will always output master

Running queries using osql

When executing any one of the following commands:
osql -E -S ComputerName\InstanceName
osql -E -S ComputerName\InstanceName -i MyScript.sql -o MyOutput.rpt
osql -E -q "SELECT * FROM Northwind.dbo.Shippers"
osql -E -Q "SELECT * FROM Northwind.dbo.Shippers" -o MyOutput.rpt
I am getting the following error:
[SQL Server Native Client 10.0]SQL Server Network Interfaces: Connection
string is not valid [87].
[SQL Server Native Client 10.0]Login timeout expired
[SQL Server Native Client 10.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.
However, I am able, without issue to login and run SELECT queries from SSMS.
How do I run queries against SQL Server 2008 using osql?
Do you have your logged in account set up as a user in SQL Server?
I usually work with specific accounts and SQL Server logins instead of Trusted Logins, and then just specify the database coordinates on the command line with the -S, -D, -U, and -P options:
osql -S %SERVERNAME% -U %USERNAME% -P %PASSWORD% -d %DBNAME%
For instance, if your server name is MyServer\SQL2008 and your user name is Foo and your password is Bar and your database is MyDB, then you'd use this:
osql -S MyServer\SQL2008 -U Foo -P Bar -d MyDB
And then continue on with the rest of your options after that.
If you really want to use your Trusted connection, you need to go to SQL Server Management Studio, and ensure your current Widows Login is added as a user and given appropriate permissions to your database, etc.
In SSMS, connect to your server manually (the 'sa' user and password perhaps), and then expand the "Security" node and look at the logins. If your currently logged in Windows User isn't listed, you'll want to right-click, add new Login, and add your current user.
Then you should be able to run with a Trusted Connection.
You have to run all command in a single line
like this
osql -E -S ComputerName\InstanceName -i MyScript.sql -o MyOutput.rpt
or
osql -E -S ComputerName\InstanceName -Q "SELECT * FROM Northwind.dbo.Shippers" -o MyOutput.rpt
Now you have to see if you can log in SQL Server or if the service is up or even if the TCP/IP protocol is enable
Use the value in the Server name: field for ComputerName\InstanceName. (e.g. MYPC\SQLEXPRESS)
Type:
osql -E -S MYPC\SQLEXPRESS
You will see the interactive prompt.
Then enter your commands:
USE pubs
GO
Also, you can use sqlcmd:
sqcmd -E -S MYPC\SQLEXPRESS

'sqlcmd' utility unable to login to the database

I have a large .sql data file (4.3 GB) and I would like to execute this file on SQL Server. Doing some research, I found that I can execute this file using the following command:
sqlcmd -U username -P password -S rs-reporting-v1 -d NSTest -i D:NetSuiteDataFiles\Transactions.sql -o D:\NetSuiteDataFiles\Transactions_output_file.txt
When I execute the above command, get the following error:
Sqlcmd: Error: Microsoft SQL Server Native Client 11.0 : Named Pipes Provider: Could not open a connection to SQL Server [53]. .
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..
I've tried changing the username to domain\username but it didn't help at all. I have checked the spellings of username and password multiple times and I'm sure they're correct.
EDIT
I changed my command to the following:
sqlcmd -E -S rs-reporting-v2 -d NSTest -i D:NetSuiteDataFil
es\Transactions.sql -o D:\NetSuiteDataFiles\Transactions_output_file.txt
and now I get the following error:
Would someone please be able to let me know what I'm doing wrong here? Any help would be much appreciated.
I would also like to point out that the .sql file is creating a table and then inserting values.

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

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
The :connect command does not need the -S switch, just use
:connect "SQLSERVERNAME"
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!

Could not connect to SQL Server using osql

I have a script that connects to SQL Server 2005 default instance. But I'm and being connected and instead got the following message
[SQL Native Client]Named Pipes Provider: Could not open a connection to SQL Server [53].
[SQL Native Client]Login timeout expired
[SQL Native Client]An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections.
The script's content is:
osql -E -S <servername> -i D:\scripts\script1.sql -o D:\scripts\script1.txt
I looked at the network connection from SQL Server Configuration Manager. The TCP/IP is enabled but the Named Pipes is disabled.
My question is what could be the possible cause and solution for this error?
try osql -E -S(local) or osql -E -S. I have not figured out why sometimes it does work, and other times it does not.