I am using this query in bat file to back-up db,
sqlcmd -S .\MSSQLSERVER -E -Q "EXEC sp_BackupDb #backupLocation='D:\SQLBackups\', #databaseName='TestDb', #backupType='F'" pause
It is throwing this error,
Sqlcmd: Error: Microsoft ODBC Driver 11 for SQL Server : SQL Server
Network Inte rfaces: Connection string is not valid [87]. 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 in stance-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 i f SQL Server is
configured to allow remote connections. For more information see SQL
Server Books Online..
Can someone please help? Thanks in advance.
you can use SQLCMD in a .bat file to make this bakcup, in this link you have an example stackoverflow link
SQLCMD:
The sqlcmd utility lets you enter Transact-SQL statements, system procedures, and script files at the command prompt, in Query Editor in SQLCMD mode, in a Windows script file or in an operating system (Cmd.exe) job step of a SQL Server Agent job. This utility uses OLE DB to execute Transact-SQL batches.
here is the link for examples and definition: msdn link definition
for example:
sqlcmd -E -S %SERVERNAME% -d master -Q "BACKUP DATABASE [%DATABASENAME%] TO DISK = N'%BACKUPFILENAME%' WITH INIT , NOUNLOAD , NAME = N'%DATABASENAME% backup', NOSKIP , STATS = 10, NOFORMAT"
hope this help!
Related
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
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
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..
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.
When I am running this from cmd on my SQL Server 2008 instance:
sqlcmd -U sa -S mymachinen_name\MSSQLSERVER
(where MSSQLSERVER is my instance name)
I get prompt for password and after that I get this:
Password:
HResult 0x57, Level 16, State 1 SQL Server Network Interfaces: Connection string is not valid [87].
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.
I have my SQL Server browser service started and also I can login through Management Studio with Windows authentication.
Also found this KB article, but this is for SQL Server 2005/2000.
Typically, the MSSQLSERVER service is the service of a un-named default instance.
Therefore, try this:
sqlcmd -U sa -S mymachine_name
(without specifying any instance name - just the machine name)
I encountered this error trying to connect to the server through Jenkins and resolved it this way:
sqlcmd -e -S "SERVER_NAME,PORT_NUMBER" -U %USER% -P %PASSWORD% -i "D:\\test.sql"
Please note that when connecting via sqlcmd to another server, if the target server is part of a HA cluster group, you will want to specify -M. Also, you will want to not specify the DB that is using the AG. (this is useful for installing jobs via sqlcmd to remote locations, among other things)
sqlcmd -S SERVERNAME,1433 -d msdb -Q "SELECT job_id FROM msdb.dbo.sysjobs WHERE (name =
N'RANDOM_JOB_NAME')" -M
It is set on installation, there is no default. Try to log on without a username