I'm looking for a way to make the below commands a bit more automated. I would like to be able to use just a simple batch file if possible.
sqlcmd /S localhost\SQLEXPRESS /U [dbusername] /P [dbpassword]
1> drop database datastore
2> go
Have you tried this:
sqlcmd /S localhost\SQLEXPRESS /U [dbusername] /P [dbpassword] <cmds.txt
where cmds.txt is a file containing your commands (the drop and the go commands).
Related
I have created the below batch file for backing up mysql individual tables, procedures and triggers from a database and then compress it using 7zip. when I run this in command prompt of windows it works perfectly and creates a file with name 20192004-1859.zip file size is few hundred mb But when I schedule it using the windows task scheduler it creates a file by name 20192004-.7z and the file size is just 1kb and empty too.Please find the code below and let me know how to make it work.
#ECHO Off
Echo Avoid Any MYSQL Transcations until backup completes
set TIMESTAMP=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%-%TIME:~0,2%%TIME:~3,2%
md E:\backup\%TIMESTAMP%\Adatabase
cd D:\wamp\bin\mysql\mysql5.7.9\bin\
mysql -s -e "SHOW TABLES FROM Adatabase" --user=root --password= --skip-column-names > E:\backup\tables.txt
for /f %%A in (e:\backup\tables.txt) DO (mysqldump --user=XXX --password=XXX Adatabase %%A > E:\backup\%TIMESTAMP%\Adatabase\%%A.sql)
mysqldump --user=root --password= --no-create-db --no-create-info --no-data --routines Adatabase > E:\backup\%TIMESTAMP%\Adatabase\procNtrig.sql
E:
cd E:\backup\
C:\7zip\7za a %TIMESTAMP%.zip e:\backup\%TIMESTAMP%\*
RMDIR /S /q e:\backup\%TIMESTAMP%
The following question has helped me solving the problem of executing multiple SQL Scripts located in file. Run all SQL files in a directory
However, I did not get how to redirect the output into a separate log file. Someone suggested the following script but since I don't understand it, it did not work and I can't find out the error.
for %f in (*.sql) do sqlcmd /S <servername> /d <dbname> /E /i "%f" >> sql.log 2>&1)
If you need the output into one common file then you should use the #Abhishek 's answer.
If you need the output into a separate log file for an each input sql file
then you can use -o parameter of sqlcmd command. Your bat file could look like this:
for %%G in (*.sql) do sqlcmd /S <servername> /d <dbname> -E -i"%%G" -o C:\logs\%%G.log
pause
In this case for
1.sql
2.sql
you will get:
1.sql.log
2.sql.log
You are seeking Command Redirection.
As per your example -
for %f in (*.sql) do sqlcmd /S <servername> /d <dbname> /E /i "%f" >> sql.log 2>&1
once the execution of the sql script is done the output will be redirected to and appends the command output to the end of file (here sql.log) without deleting the information that is already in the file (>>) and redirects STDERR (2) into STDOUT handle(1) - 2>&1
More information here and here.
I have a batch file that runs multiple SQLCMD commands which run queries supplied by external SQL script files. The scripts themselves are fairly trivial, but I'm bugged by all the file dependencies required to run the batch. I've been googling for ways to link all these files together without luck.
Are there any techniques I can apply to encapsulate the batch script and SQL scripts within a single file, such that I could move the resulting file to other machines without dragging the individual sql files along? For the sake of organization/readability/information hiding, I would really prefer to avoid embedding the sql directly in the batch file.
Consider using 7-Zip. It is free and here
You could encapsulate all your BAT files and SQL commands into a zip like this:
C:\> 7za a -t7z archive.7z *.BAT *.SQL
Then you can extract like this:
C:\> 7z e archive.7z
Alternatively you could make a self-extracting executable with it - which has the benefit you don't need any tools to extract it when you arrive at another server, but you may not be able to Email that. By the way, if you change the command to the following, Windows can read the archive itself natively so you don't need to install extraction software wherever you go with the file:
C:\> 7za a -tzip archive.zip *.BAT *.SQL
Or you could use the Microsoft CAB tool that will be present everywhere documentation.
That would look something like this if you wanted to gather *.BAT and *.CMD into a "cabinet" called BATplusSQL.cab:
dir /b *.BAT *.CMD >files.txt
makecab /d "CabinetName1=BATplusSQL.cab" /f files.txt
del /q /f files.txt
Here is an example of what I mean.
Create a text file with one SQL query per line similar to this:
My Queries
Select * from table1 where something = somethingelse
Select * from table2
delete from table3
update table1 set something=nothing
Make sure the first line is not a query. It can be anything else. Tweak the settings in %sql% as necessary.
Run the following batch file:
#echo off
setlocal
set "sql=sqlcmd -S MySERVER -E -q "
set cnt=1
for /f "tokens=* skip=%cnt%" %%a in (sqlQueries.txt) do (
echo %sql% "%%a"
set /a cnt+=1
)
Remove the echo to run the queries.
I am using sql server 2008 , I am developing script that get all .sql file of given path ( also serch in subfolder recursively). Thanks in advance.
You could use a batch file like this. Call it ashwin.bat (or whatever you like) and it will look for all the files in C:\tmp\so\ashwin that have a .sql extension and then invokes sqlcmd against all of those files against a named instance database of localhost\localsqla and runs them in the master database.
#echo off
For /R "C:\tmp\so\ashwin\" %%i in (*.sql) DO CALL sqlcmd.exe -E -S localhost\localSQLA -d master -i %%i
A litle enhancement for logging purposes:
#echo off
For /R "C:\Deploy\SQL" %%i in (*.sql) DO CALL echo %%i && sqlcmd.exe -E -S DB_IP -d DATABASE -i %%i -j
how do I write my file directory to a text file? The directory I want to use is C:\
The code I have now is
exec xp_cmdshell 'dir *.exe & echo > file_directory.txt';--
It's not writing to a file though. Do I need to say this...
exec xp_cmdshell 'cd c: \ & dir *.exe & echo > file_directory.txt';--
cd (by itself) only sets the working directory, it won't change the working disk drive. If you (may) need to change the working drive and working directory you need pushd c:\ or cd /d c:\.
The easier solution is to fully-qualify the file name instead:
echo > c:\file_directory.txt
But it's generally not a good idea to write to the root of C:. Microsoft has tried to make this harder, for the very good reason that opening up write privilege to the root of your system partition opens up all sorts of security risks.
(Update:)
I think what you want is
exec xp_cmdshell 'dir *.exe > c:\file_directory.txt';