0

I would like to save the result to a csv file.

I executed the following command.

sqlcmd -S localhost -d my_db-E -Q “SELECT TOP 10 * FROM [my_db].[dbo].[my_table]” -o “C:\CSVData.csv” -W -w 1024 -s”,”

Then, I opened the csv file but it only contains three error lines.

What is the correct approach?

Thanks.

Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Named Pipes Provider: Could not open a connection to SQL Server [2]. .

Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Login timeout expired.

Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : 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..
jarlh
  • 42,561
  • 8
  • 45
  • 63
Chan
  • 3,605
  • 9
  • 29
  • 60
  • Are those the double quotes you're using? `“ ... ”` ? – Cid Feb 19 '19 at 08:20
  • 1
    Add a space between `my_db` and `-E` -> `-d my_db-E` – Cid Feb 19 '19 at 08:21
  • Take a look on spaces also a dot instead of localhost: `sqlcmd -S. -dmy_db -E` – Alexander Volok Feb 19 '19 at 08:27
  • The error complains that it can't connect to the server. This has nothing to do with exporting to CSVs. It means that the server isn't running, that there's no server installed locally, or that the server is a named instance, eg SQL Server Express. You need to include the instance name, eg `.\SQLEXPRESS` or `localhost\SQLEXPRESS`. Apart from that, you're trying to connect to database `my_db-E ` without any credentials. I suspect you wanted to write `-d my_db -E` – Panagiotis Kanavos Feb 19 '19 at 09:01
  • Possible duplicate of [Why am I getting "Cannot Connect to Server - A network-related or instance-specific error"?](https://stackoverflow.com/questions/18060667/why-am-i-getting-cannot-connect-to-server-a-network-related-or-instance-speci) – Panagiotis Kanavos Feb 19 '19 at 09:01
  • Can you show me an example on how to write the correct command? – Chan Feb 19 '19 at 09:15

0 Answers0