SQLCMD can't connect when in shell script - sql-server

I'm actually writing a little script that call sqlcmd.
There it is :
sqlcmd -S localhost\SQLEXPRESS -E -i Select_All.sql -o All_Tables_Name.txt
When i write this as a cmd line it works well.
When i put it in a .sh file sqlcmd can't connect to the Database and i get these errors :
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 tried to change the -S argument with the Machine name or by '.' but still doesn't work.
Can someone explain me why is it different when i write it as a command line and when i launch it from a script Please ? (like "sh MyScript.sh")
Thank you.

Related

Getting Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server When trying to run a batch script

I am using XAMPP for creating a local SQL Server instance to run a SQL script. the issue here is, when I run the script I get the following error. What is going wrong here?
Code:
sqlcmd -S localhost -d mysqldb -U uname -P pass -i C:\query2.sql
PAUSE
Error:
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Named Pipes Provider: Could not open a connection to SQL Server [2].
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 17 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.
sqlcmd is for SQL Server, you i couldn't connect to MySQL with it as #james Z mentioned. They are completely different database products. I tried using mysql commmand
C:\Software\XAMPP\mysql\bin\mysql --host=hostname --port=portnumber -D dbname -u username --password=**** < path to sql file

Error when trying to schedule auto backup in SQL Server Express

I am trying to create scheduled task for auto backup in SQL Server 2019 based on this article :
https://learn.microsoft.com/en-us/troubleshoot/sql/admin/schedule-automate-backup-database
As recommended in order to test if the auto backup is working, I have to run the batch file containing this command:
sqlcmd -S .\EXPRESS -E -Q "EXEC sp_BackupDatabases #backupLocation='c:\AutoBackup\', #backupType='F'"
I ran it in the command line to test if its working fine but I get the following error:
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : MAX_PROVS: Error Locating Server/Instance Specified [xFFFFFFFF]. .
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 17 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..
I installed Sql server ODBC driver for another issue that I faced before and also checked that remote connection is allowed as SSMS is working fine.
Any advice is appreciated.
Have you double checked that your SQL Express instance is actually named "EXPRESS"?
When you connected using SSMS, did you also used Windows Authentication?
Your command breaks down as follows:
This part defines the server/instance. IN this case on the local machine with a Named Instance of "EXPRESS"
sqlcmd -S .\EXPRESS
This part states that you want to use Windows Auth with the currently logged in user
-E

How do I use a Tidal Scheduler variable in sqlcmd?

We're using Tidal 6.5.3.164. I'm running a SQL Server stored procedure using sqlcmd. It works when I specify the server but I want to use a Tidal variable for the server and it's not working. In the Command line I have sqlcmd. In the Command Parameters I have:
-E -S <Group.Report_Server> -Q "exec dbo.test_sproc"
I get the following error messages:
Sqlcmd: Error: Microsoft ODBC Driver 11 for SQL Server : Named Pipes Provider: Could not open a connection to SQL Server [53]. .
Sqlcmd: Error: Microsoft ODBC Driver 11 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 11 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..
My guess is sqlcmd is trying to connect to a server named <Group.Report_Server> because it works fine when I specify the actual server name. Any idea's how to use the variable with sqlcmd?
The problem was the Tidal variable was defined several levels higher and not in scope when I ran this. Normally this would run fine but the way I was trying to run it didn't work. Everything works as expected, I just had to learn more about Tidal.

Unable to write SQL result to csv file

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..

How can I log into SQL Server via SQLCMD using Git Bash?

In my command prompt this seems working perfectly:
sqlcmd -S .\sqlexpress
But in my Git Bash this does not seem to work. It's giving:
Named Pipes Provider: Could not open a connection to SQL Server [53].
Sqlcmd: Error: Microsoft 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..
Sqlcmd: Error: Microsoft SQL Server Native Client 10.0 : Login timeout expired.
As I stay in my Git Bash a lot, how can I log into my SQL Server via Git Bash without moving around often?
You need to escape the backslash separating the instance name in the git bash shell with an additional backslash:
SQLCMD -S .\\sqlexpress
I guess I needed winpty for making login working:winpty sqlcmd -S .\\sqlexpress

Resources