Batch file that runes SQL commands - batch-file

I want to create a batch file that runs some SQL commands for example:
I'm signing in using these commands:
Sqlcmd -S <Server Location> -U <user> -P <password>
Next is going to be a backup for some database
BACKUP DATABASE <data base> TO DISK=<Location>
GO
EXIT
But after starting the .bat file, log in is successfully but the backup database is not working, I still need to type the commands.
There is a way to actually run those commands without typing in the commands?
Or should I try using something else, like Windows PowerShell?

Related

batch file - sqlcmd failing in scheduled tasks (0x2331)

I'm trying to use scheduled tasks to run a database back up once per day using sqlcmd that points to a SQL script.
If I open a command prompt and run the code in the batch file everything is successful but when task manager tries it gets a 0x2331 error.
A search reveals it's something to do with permissions - maybe I need to change the path to the sql file?
here is the script:
ECHO OFF
ECHO This is running to backup a database
sqlcmd -S COMPNAME -i C:\backup.sql
GO
ECHO Success!
EXIT
I'm new to batch files to tried to keep it as simple as possble.

automate the following steps

I have created an .bat file with the following command with a text file which contain "sudo su - oradev2"(C.txt).
cd C:\Users\chakraborty_sayantan\Desktop
plink -ssh serverA -l username -pw password -m "C:\Users\username\Desktop\c.txt" -t
The command above completes the first step of logging into a DB and the c.txt file has the instruction to enter into sudo mode. However, post this there is an authentication. Is there a way to automate the step of entering the password? Any thoughts?
Procedure to automate
login
password
sudo su - oradev
password
echo $ORACLE_SID
sqlplus / as sysdba
create user identified by
default tablspace
The above steps need to get automate using a bat file which consist of plink/putty.
-Sayantan
You can make a password and username txt file and maybe use the IF NOT EXIST Command to check if that user and pass file exists and set some commands up to create that file
Hope this is what you meant :)

How do I create a batch file from Advantage SQL script?

I have created sql script using SQL Utility within Advantage Data Architect of Sybase. The script is saved on my workstation. Now, how do I create a batch file that would run the script from desktop?
I found this command line online, but it doesn't seem to be working:
#echo on
isql -U "username" -P "password" -S "servername" -D "database" -i "path"
#echo off
I am new to Advantage SQL, trying to learn as much as I can.
Thanks.
You use adssqlcmd.exe, passing the connection and script name as command-line parameters. There is an example of doing so on that second page:
rem Using the connection path option, and process the script files
rem after making the connection.
rem The program will terminate after processing all files
asqlcmd.exe -S ALS:d:\mydata\main.add -U user1 -P sample -i myscript.sql
There's a list of supported commands that outline what you can and can't do in the script as well.
Also note that adssqlcmd.exe is a feature added in ADS v11, and is not available to earlier versions of ADS.

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