DOS batch file - loop and increment by 1 - sql

I have this batch file that logs into sql on a remote machine runs a stored procedure and then sends the output to a text file. I would like it to increment both the 3rd octet in the IP address and the name of the output text file by 1 and loop so I don't have to repeat the command over and over. Also, I would like it to stop when it reaches a certain number. Is there a way to do this?
sqlcmd -U user -P password -S 192.168.1.2 -i c:\sql\storecreditfix.sql -o c:\sql\ouput01.txt
sqlcmd -U user -P password -S 192.168.2.2 -i c:\sql\storecreditfix.sql -o c:\sql\ouput02.txt
sqlcmd -U user -P password -S 192.168.3.2 -i c:\sql\storecreditfix.sql -o c:\sql\ouput03.txt
sqlcmd -U user -P password -S 192.168.4.2 -i c:\sql\storecreditfix.sql -o c:\sql\ouput04.txt
sqlcmd -U user -P password -S 192.168.5.2 -i c:\sql\storecreditfix.sql -o c:\sql\ouput05.txt

this will do what you want. the /L specifier tells it to act like a regular programming for loop. the first parameter is the starting integer, the next is the number to step, and the last is the count. so this will loop starting at 1, incrementing by 1 for 6 integers:
#echo off
FOR /L %%G IN (1, 1, 6) DO (
echo sqlcmd -U user -P password -S 192.168.%%G.2 -i c:\sql\storecreditfix.sql -o c:\sql\ouput0%%G.txt
)
and if you want to do a list of IPs instead of a range, you can leave off the /L and just list the numbers. e.g.
FOR %%G IN (1,2,3,99,121) DO ...
obviously the "echo" before sqlcmd is just for testing;)

On the assumption that you're actually using cmd.exe rather than MS-DOS, one way to increment and test a variable is as follows:
#setlocal enableextensions enabledelayedexpansion
#echo off
set /a "i = 1"
:loop
if !i! leq 15 (
if !i! lss 10 (
echo sqlcmd -S 192.168.!i!.2 -o c:\sql\ouput0!i!.txt
) else (
echo sqlcmd -S 192.168.!i!.2 -o c:\sql\ouput!i!.txt
)
set /a "i = i + 1"
goto :loop
)
endlocal
This is a slightly modified version of what you need which echoes the relevant bits rather than executing them, and it outputs:
sqlcmd -S 192.168.1.2 -o c:\sql\ouput01.txt
sqlcmd -S 192.168.2.2 -o c:\sql\ouput02.txt
sqlcmd -S 192.168.3.2 -o c:\sql\ouput03.txt
sqlcmd -S 192.168.4.2 -o c:\sql\ouput04.txt
sqlcmd -S 192.168.5.2 -o c:\sql\ouput05.txt
sqlcmd -S 192.168.6.2 -o c:\sql\ouput06.txt
sqlcmd -S 192.168.7.2 -o c:\sql\ouput07.txt
sqlcmd -S 192.168.8.2 -o c:\sql\ouput08.txt
sqlcmd -S 192.168.9.2 -o c:\sql\ouput09.txt
sqlcmd -S 192.168.10.2 -o c:\sql\ouput10.txt
sqlcmd -S 192.168.11.2 -o c:\sql\ouput11.txt
sqlcmd -S 192.168.12.2 -o c:\sql\ouput12.txt
sqlcmd -S 192.168.13.2 -o c:\sql\ouput13.txt
sqlcmd -S 192.168.14.2 -o c:\sql\ouput14.txt
sqlcmd -S 192.168.15.2 -o c:\sql\ouput15.txt
Simply take the echo off the line and adjust the command to put back the other bits.

If you have access to cmd.exe then you also have access to cscript which allows you to write a DOS batch file in Javascript.

Related

Stored procedure execution

I have written a stored procedure to be called from sqlcmd (batch file). This is the code - for some reason it is not executing.
#ECHO OFF
SET /P FDate=Enter From Date:
SET /P TDate=Enter To Date:
ECHO sqlcmd -E -Q "dbo.SavingsAccountsAllDetail #FDate=N'%Param1%', #TDate=N'%Param2%'" -S OMNIDB-UAT -d HNBG_LOAN_TEST -o C:\SavingsAccountsAllDetailRepo.txt
SET Param1=
SET Param2=
Any thoughts?
Try to add EXEC before procedure's name:
sqlcmd -E -Q "exec dbo.SavingsAccountsAllDetail #FDate=N'%Param1%', #TDate=N'%Param2%'" -S OMNIDB-UAT -d HNBG_LOAN_TEST -o C:\SavingsAccountsAllDetailRepo.txt
You can also try to passparameters via the /v argument. More datais here
sqlcmd -E -Q "exec dbo.SavingsAccountsAllDetail #FDate=N'$(Param1)', #TDate=N'$(Param2)'" /v Param1=%Param1% Param2=%Param2% -S OMNIDB-UAT -d HNBG_LOAN_TEST -o C:\SavingsAccountsAllDetailRepo.txt

Change the name of the CSV File Automatically

sqlcmd -S PC03 -d db_test -E -o "test\MyData.csv" ^
-Q "[test2]" ^
-W -w 999 -s","
I would like to change the name of the file into "20150512". The name of the file should be today's date.
I do not know how to do it.
Thanks!
try this to generate YYYYMMDD format output file. (append your filename)
Edited:
sqlcmd -S PC03 -d db_test -E -o c:\test-%datetime:~0,4%-%datetime:~4,2%-%datetime:~6,2%.csv
sqlcmd -S servername -d dbtest -E -o c:\asdf\%date:~0,10%.csv ^ -Q
"[dbname].[sp]" ^ -W -w 999 -s","

How to pass a bash variable into an sql file

I have a bash script to call a select in postgres. I would like to be able to pass a variable from the command line into the sql file.
sh myscript.sh 1234
#!/bin/bash
dbase_connect="psql -h server -U username dbase"
file="/tmp/$fname.csv"
sql="/home/user/sql_files/query.sql"
sudo bash -c "$dbase_connect -c -q -A -F , -f $sql -o $file"
The query can be as simple as:
select name from table where id = $1;
But I don't know how to call the $1 into the sql file. The actual query is much larger and I prefer to keep it out of the bash query itself because it is easier to maintain when called as a seperate .sql file.
you can use sed to insert parameter :
#!/bin/bash
dbase_connect="psql -h server -U username dbase"
file="/tmp/$fname.csv"
sql="/home/user/sql_files/query.sql"
tmp="home/user/sql_files/query.sql.tmp"
s="s/\$1/$1/g"
cat $sql | sed $s > $tmp
sudo bash -c "$dbase_connect -c -q -A -F , -f $tmp -o $file"
rm -f $tmp

Stopping executing of SQL scripts after error occurs

I am executing multiple SQL scripts using batch file but now I want to stop execution of batch file if error occurs in any one of the scripts.
This is my batch file:
sqlcmd -S dbdev026b\dbdev026b -i c:Test1.sql -o c:\o1.txt
sqlcmd -S dbdev026b\dbdev026b -i c:Test2.sql -o c:\o2.txt
sqlcmd -S dbdev026b\dbdev026b -i c:Test1.sql -o c:\o1.txt || exit /b 1
sqlcmd -S dbdev026b\dbdev026b -i c:Test2.sql -o c:\o2.txt || exit /b 2

SQL Server 2008 automated database drop, create and fill

For the database in my project I have a drop/create script for the database, a script for creating tables and SPs and an Access 2003 .mdb file with some exported values.
To set up the database from scratch I can use my SQL management studio to first run one script, then the other and lastly manually run the sort of tedious import task.
But I would like to do this as automated as possible. Hopefully something like putting the three files in a folder along with a fourth script to execute. Looking something like:
run script "dropcreate.sql"
run script "createtables.sql"
import "values.mdb"
How is this done? I hope to avoid using SSIS and the like. The tricky this is of course the import of data, where I can't seem to find a simple way. It is also important that the files a left as they are and not embedded into anything.
:: DOC AT THE END
#ECHO OFF
::BOOM BOOM BOOM CHANGE THIS ONE WHEN YOU ARE INSTALLAING DIFFERENT DATABASE
SET DbName=CAS_DEV
ECHO CREATE FIRST BACKUP OF ALL DATABASES ON THE DEFAULT INSTANCE ONES:
ECHO CREATING THE LOG FILES
echo THIS IS THE ERROR LOG OF THE UPDATE OF THE %DbName% ON %DATE% >error.log
echo THIS IS THE INSTALL LOG OF THE UPDATE OF THE %DbName% ON %DATE% >install.log
ECHO STARTTING BACKUP
CD .\0.BackUp
ECHO FOR EACH SQL FILE DO RUN IT THIS WILL TAKE A WHILE
ECHO SINCE WE ARE GOING TO MAKE A BACKUP FOR ALL THE DATABASES ON THE CURRENT HOST
for /f %%i in ('dir *.SQL /s /b /o') DO ECHO %DATE% --- %TIME% RUNNING %%i 1>>"..\install.log"&SQLCMD -U ysg -P pass -H hostname -d MASTER -t 30000 -w 80 -u -p 1 -b -i %%i -r1 1>> "..\install.log" 2>> "..\error.log"
ECHO GO ONE FOLDER UP
ECHO SLEEP FOR 1 SECOND
ping -n 1 127.0.0.1 >NUL
ECHO DONE WITH BACKUP GOING UP
cd ..
ECHO THE BACKUPS ARE IN THE FOLDER
ECHO D:\DATA\BACKUPS
ECHO CLICK A KEY TO CONTINUE
ECHO ========================================================================================================================
PAUSE
ECHO STARTING INSTALLING FUNCTIONS
CD ".\1.Functions"
ECHO FOR EACH SQL FILE DO RUN IT
ping -n 1 127.0.0.1 >NUL
for /f %%i in ('dir *.SQL /s /b /o') DO ECHO %DATE% --- %TIME% RUNNING %%i 1>>"..\install.log"&SQLCMD -U ysg -P pass -H hostname -d %DbName% -t 3000 -w 80 -u -p 1 -b -i "%%i" -r1 1>> "..\install.log" 2>> "..\error.log"
ECHO DONE WITH STORED PROCEDDURES GOING UP
cd ..
ping -n 1 127.0.0.1 >NUL
ECHO HIT A KEY AFTER PAUSE
PAUSE
ECHO START TO EXECUTE THE MIXED FILES
CD .\1.Mixed
ECHO CREATING THE LOG FILES
echo. >>"..\error.log"
echo. >>"..\install.log"
ECHO FOR EACH SQL FILE DO RUN IT
for /f %%i in ('dir *.SQL /s /b /o') DO ECHO %DATE% --- %TIME% RUNNING %%i 1>>"..\install.log"&SQLCMD -U ysg -P pass -H hostname -d %DbName% -t 3000 -w 80 -u -p 1 -b -i %%i -r1 1>> "..\install.log" 2>> "..\error.log"
ECHO GO ONE FOLDER UP
cd ..
ECHO SLEEP FOR 1 SECOND
ping -n 1 127.0.0.1 >NUL
ECHO DONE WITH MIXED GOING UP
ECHO HIT A KEY AFTER PAUSE
PAUSE
ECHO STARTING INSTALLING TABLES
CD .\2.Tables
ECHO FOR EACH SQL FILE DO RUN IT
ping -n 1 127.0.0.1 >NUL
for /f %%i in ('dir *.SQL /s /b /o') DO ECHO %DATE% --- %TIME% RUNNING %%i 1>>"..\install.log"&SQLCMD -U ysg -P pass -H hostname -d %DbName% -t 3000 -w 80 -u -p 1 -b -i "%%i" -r1 1>> "..\install.log" 2>> "..\error.log"
ping -n 1 127.0.0.1 >NUL
ECHO DONE WITH TAbles GOING UP
cd ..
ping -n 1 127.0.0.1 >NUL
ECHO HIT A KEY AFTER PAUSE
PAUSE
ECHO STARTING INSTALLING Views
CD ".\3.Views"
ECHO FOR EACH SQL FILE DO RUN IT
ping -n 1 127.0.0.1 >NUL
for /f %%i in ('dir *.SQL /s /b /o') DO ECHO %DATE% --- %TIME% RUNNING %%i 1>>"..\install.log"&SQLCMD -U ysg -P pass -H hostname -d %DbName% -t 3000 -w 80 -u -p 1 -b -i "%%i" -r1 1>> "..\install.log" 2>> "..\error.log"
ECHO DONE WITH Views GOING UP
cd ..
ping -n 1 127.0.0.1 >NUL
ECHO HIT A KEY AFTER PAUSE
PAUSE
ECHO STARTING INSTALLING stored procedures
CD ".\5.StoredProcedures"
ECHO FOR EACH SQL FILE DO RUN IT
ping -n 1 127.0.0.1 >NUL
for /f %%i in ('dir *.SQL /s /b /o') DO ECHO %DATE% --- %TIME% RUNNING %%i 1>>"..\install.log"&SQLCMD -U ysg -P pass -H hostname -d %DbName% -t 3000 -w 80 -u -p 1 -b -i "%%i" -r1 1>> "..\install.log" 2>> "..\error.log"
ECHO DONE WITH STORED PROCEDDURES GOING UP
cd ..
ping -n 1 127.0.0.1 >NUL
ECHO HIT A KEY AFTER PAUSE
PAUSE
ECHO STARTING INSTALLING Triggers
CD ".\6.Triggers"
ECHO FOR EACH SQL FILE DO RUN IT
ping -n 1 127.0.0.1 >NUL
for /f %%i in ('dir *.SQL /s /b /o') DO ECHO %DATE% --- %TIME% RUNNING %%i 1>>"..\install.log"&SQLCMD -U ysg -P pass -H hostname -d %DbName% -t 3000 -w 80 -u -p 1 -b -i "%%i" -r1 1>> "..\install.log" 2>> "..\error.log"
ping -n 1 127.0.0.1 >NUL
ECHO DONE WITH Triggers GOING UP
cd ..
ping -n 1 127.0.0.1 >NUL
ECHO HIT A KEY AFTER PAUSE
PAUSE
ECHO Please , Review the log files and sent them back to Advanced Application Support
set mailadd= yordan.georgiev^#oxit.fi
:: WE USE THE "%cd%\bin\bmail.exe".EXE UTILITY TO SEND OURSELF AN E-MAIL CONTAINING THE TEXT FILE
:: ALTERNATIVE SMTP MIGHT BE company.com, UNCOMMENT THE NEXT LINE FOR ALTERN
::cmd /c "%cd%\bin\bmail.exe" -s company.com -m %computername%.txt -t %mailadd% -a %computername% -h
::"%cd%\bin\bmail.exe" -s smtp.company.com -m install.log -t yordan.georgiev#oxit.fi -a "POC 1.2 install log" -h
::"%cd%\bin\bmail.exe" -s smtp.company.com -m error.log -t yordan.georgiev#oxit.fi -a "POC 1.2 error log" -h
cmd /c start /max INSTALL.LOG
CMD /C start /MAX ERROR.LOG
echo DONE !!!
ECHO HIT A KEY TO EXIT
PAUSE
:: WE GO TROUGH ALL THE FOLDERS AND RUN THE SQL FILES IN ALPHABETIC ORDER
You can run SQL Server Management Studio in SQLCMD mode. In there you can run scripts as follows
:r c:\temp\DropCreate.SQL
:r c:\temp\CreateTables.SQL
Alternately, you can run the whole thing from a batch file using SQLCMD.exe commands.
SQLCMD -S "." -E -i "c:\temp\DropCreate.SQL"
SQLCMD -S "." -E -i "c:\temp\CreateTables.SQL"
Do you have an alternative to SSIS that can import the data for you? Usually to do any kind of transformations and loading you need error handling, lookups, etc that you will have to code yourself unless you use an off-the-shelf product.
You can read a lot about SSIS right here on SO.
We have a similar project (create db, load data, create code). We do all of this inside a Database Project - with Visual Studio Team System Edition 2008 and GDR2.