I was able to perform backup for my SQL Server Express using this command at the command prompt:
SQLCMD -E -S testing\SQLEXPRESS –Q "BACKUP DATABASE testing3 TO DISK='C:\Users\Administrator\Desktop\test.bak'"
When I want to create a batch script using the command above in order to create a task scheduler to perform auto backup, it shows error as below:
Here is the batch code:
echo off
c: \
CD "C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn"
SQLCMD -E -S testing\SQLEXPRESS –Q "BACKUP DATABASE testing3 TO DISK='C:\Users\Administrator\Desktop\test.bak';"
I didn't put exit at bottom of it because I want to verify whether the command successfully run or not.
ERROR:
Sqlcmd: 'ûQ "BACKUP DATABASE testing3 TO DISK='C:\Users\Administrator\Desktop\test.bak' ': Unexpected argument. Enter '-?' for help.
Does anyone know how to fix this?
EDIT : I solve it by giving a semi column at the end and type it one by one instead of copy paste it
Related
I have a .bak file and want to import it back again into SQL Server from a Windows cmd prompt.
I tried:
sqlcmd -S "MyComputer\MyServer" -U myuser -P mypass -d database
-i "C:\Program Files\Microsoft SQL Server\myfiles\mybackup.bak"
But the result is only:
Sqlcmd: Error: Syntax error at line 3917 in file 'C:\Program Files\Microsoft SQL Server\myfiles\mybackup.bak'.
Restoring the same file via Microsoft SQL Server Management Studio works without problems. What am I missing?
I use docker-compose.yml to load my SQL Server image inside a container.
After it's up and running, I create a command.sh shell and try to run it to create a database.
# command.sh
echo 'creating database from ->' $ModuleName
export query="'create database $ModuleName'"
echo $query
/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P my_strong_password -Q $query
And it gives me this error:
Sqlcmd: 'database': Unknown Option. Enter '-?' for help.
Please note that I can't use -i switch to use an input .sql file, because I'm creating my queries programmatically in shell based on environment variables.
The output of sqlcmd -? shows how to use then -Q option. On windows this says [-Q "cmdline query" and exit].
But Windows and Linux differ (or are not consistent) in the use of single- or double quotes.
First option is to try: sqlcmd -Q "\"create database $ModuleName\""
Second option is:
Create a temporary file (i.e. /tmp/tmp.sql), and put the SQL statement in that script.
Use -i /tmp/tmp.sql to execute that script.
I have a generated script file with 1.7 GB size, I'm trying to run this script using the following sqlcmd
sqlcmd -m1 -S 192.168.100.10\SQLHQ -U sa -P 123456 -i "E:/VMS2008R2.sql"
When I run this line in CMD as administrator, the command take some time and give me no error, the database schema generated to database but without data and without any success or finished message!
UPDATE
When I run the command without -m1 I got the following exception:
TCP Provider: An existing connection was forcibly closed by the remote host.
Communication link failure
I have an SQL script file which it has big size " 1.4 GB "
when I try to open it in SQL SERVER Management Studio
it fails and give me message
The System can't find the file specified
when I try to open it with notepad++
file is too big to be opened by notepad++
so does it there anyway to open this script ?
Run this command in Command prompt by providing the required details. This command would execute the SQL file without opening the file.
sqlcmd -S <ServerName> -U <LoginUser> -P <Password> -d <DatabaseName> -i <Inputfilenamewithpath> -o <Outputfilenamewithpath>
I want to run a dos script to execute the following command:
C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqldump --opt --where='1 limit 1' -h a.b.c.d -u root -proot remotedb remotetable|mysql -u root -pcanada localdb
I am not able to run this script.If I do not put double quotes around the path it gives error as 'C:\Program' is not recognized as an internal or external command.
If I put double quotes around the path then also it fails with following error:
"mysqldump: Got error: 1049: Unknown database 'limit' when selecting the database"
Could anyone suggest how I could run this query in script.It runs perfect when i run it directly through command line.Problem comes when i try to run it through batch ie .cmd file.
Thanks in advance:)
Try
"C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqldump" --opt --where="1 limit 1" -h a.b.c.d -u root -proot remotedb remotetable
You have to quote the path because it contains spaces and cmd doesn't treat single quotes as anything special.