Not able to run mysql query through batch file - batch-file

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.

Related

Why database creation command fails in SQLCMD in SQL Server docker for linux?

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.

SQL Server Express backup script

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

Isql command error while executing query on Sybase database

I would like to create a Windows batch file to execute the query on Sybase database running on Linux.
Batch file:
plink.exe -ssh sybase#<IP> -pw <PW> -m C:\scripts\script1.bat -t > C:\scripts\testing.log
script1.bat:
echo --- query 1 -----
cd /sybase/OCS-15_0/bin/
isql -Uimaldb_bkp -Pstart_bkp -SLinux1 -Dimaldb
sp_helpsegment
go
exit
It works fine until isql command and gives output in testing.log:
--- query 1 ----- bash: isql: command not found bash: sp_helpsegment: command not found bash: go: command not found
Please advice.
'isql' will work once the required environment variables for Sybase ASE are set in the database server (Linux server as mentioned by you). Please check the link below for more details:
http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.dc35823.1570/html/uconfig/X30690.htm
On a simpler note, there should be a file named 'SYBASE.sh' inside '/sybase' (I guess Sybase has been installed in this directory from your sample code). You need to source this file by editing the '.bashrc' file present inside the home directory of the user you are using to connect to the Linux server.
For the sql to work, you will need a flag to indicate the start and end of sql block in the script. Please try the following:
isql -Uimaldb_bkp -Pstart_bkp -SLinux1 -Dimaldb <<EOF
sp_helpsegment
go
EOF
You can use any other word instead of 'EOF'
I too face the same issue.
You need to insert your sybase environment variables, like below:
. /sybase/ABD/SYBASE.sh
isql -U**** -P****** -S<SID> -X <<EOF
It will work fine. Please try this.
If you don't have permission to change de server PATH, you can find the path of isql and execute this way:
/sybase/OCS-15_0/bin/isql -Uimaldb_bkp -Pstart_bkp -SLinux1 -Dimaldb
Putting the prefix PATH of isql installation (it depends of your version and path)

Running SQLCMD from a batch file after SQLServer Install

I've come across an odd problem, which I'm sure is easily explained and fixed.
I'm using a .bat file to install a lot of programs, one of which is SQL server 2012 (if it matters, the other installations are just .net framework, OPOS drivers and POS software which uses SQL server).
I can get the batch file to silently instal everything, including SQL (with SSMS), which is great. But once SQL Server is installed I'd like to run a couple of SQL scripts to create/attach databases.
I know that the install works (if I dont' try to doanything other than the installs), and I know that the SQLCMD works (if I run it seperately, after the install), but if I try to run the SQLCMD after the install, in the same batch file it fails with the standard 'SQLCMD is not recognised as an internal or external command...'
I've put this down to I need to restart CMD to get it to recoginise the new command (i.e. the SQLCMD), so I figured I'd split out the SQLCMD commands into a separate batch file and call it, but it still doesn't work. I have to physically close my original batch file down before CMD.exe picks up the new commands.
So... is it possible to 'refresh' cmd.exe so that the newly installed SQLCMD commands are useable from the original batch file??
Here is (some of) my script. (note that I've removed all of the other install before SQL Server)
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
"%~dp0SQLServer.exe" /QS /INDICATEPROGRESS /ACTION=install /FEATURES=SQL,SSMS /INSTANCENAME=Datasym /SECURITYMODE=SQL /SAPWD=Welcome21ST /IACCEPTSQLSERVERLICENSETERMS
echo.
echo SQL Server installed
sqlcmd -S %computername%\DATASYM -U sa -P Welcome21ST -i %script%
The %script% variable changes (like I said, there are multiple scripts I'd like to run). And I know that the scripts themselves work.
Why does CMD not recognise SQLCMD as a command??
I tried to replace the line:
sqlcmd -S %computername%\DATASYM -U sa -P Welcome21ST -i %script%
with
call sqlscript.bat
as I thought that would open another CMD,exe (which it does), so I assumed that the second CMD would see the ew command (SQLCMD), but it doesn't seem to .
Any help on how I can get the SQLCMD to work in the original batch file would be greatly appreciated.
As you've already found, the current CMD window won't pick up the new changes to the system PATH environment variable. If for some reason you don't want to put the full path to sqlcmd (which I think is the best solution), you might try doing this instead.
start /wait cmd /c sqlcmd -S %computername%\DATASYM -U sa -P Welcome21ST -i %script%
This will spawn a new window and wait for the sqlcmd call to finish before returning to your original script.

export MySql database with cmd

I'm using MySql 5.1 version and I'm trying to export mydatabase by cmd ,when I wrote this command :
C:\Program Files\MySQL\MySQL Server 5.1\bin\mysqdump -uroot -ppassword mydatabase >mydatabase.sql
I get an Error
Access is denied
I don't Know what the problem
Help Pleas.....
What's your permissions on the directory you're running this command from? It may be trying to write the mydatabase.sql file to a directory you don't have permissions to.
First check if your command line recognizes mysql command. If not go to command and type in
set path=c:\wamp\bin\mysql\mysql5.1.36\bin
Then use this command to export your database
Go to directory where you have mysql. -u - for username -p - to prompt the password
C:\xampp\mysql\bin>mysqldump -u Username -pPassword DatabaseName > DatabaseName.sql
Note:It is better to use full path of the the sql file file.sql
To export database from dump file (in this case called filename.sql) use: mysql -u username -p password database_name > filename.sql
If you are on Windows you will need to open CMD and go to directory where mysql.exe is installed. If you are using WAMP server then this is usually located in: C:\wamp\bin\mysql\mysql5.1.36\bin (*note the version of mysql might be different)
So you will: cd C:\wamp\bin\mysql\mysql5.1.36\bin
and then execute one of the above commands.

Resources