SQL Server 2008 automated database drop, create and fill - sql

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.

Related

ssh on remote server with nohup - not able to create files

I'm calling a shell script on a remote server with ssh ssh user#host '/dir/myscript.sh'and myscript.sh then runs another script with nohup, but the 2nd script isn't writing to nohup / to any other file with echo "" > test.txt.
myscript.sh
#!/bin/bash
shopt -s expand_aliases
source /home/user/.bash_profile
nohup /dir/myscript-real.sh > my.out 2> my.err < /dev/null &
myscript-real.sh
#!/bin/bash
shopt -s expand_aliases
source /home/user/.bash_profile
echo -e "test"
sleep 15
echo "2nd test"
echo "test" > test.out
exit
When ran via SSH - no file is being created (my.out, my.err or test.out), but when running myscript.sh directly - those 3 files are being created.
Any ideas where is the problem?
Thanks.

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

shell script not working

I have a shell script to do the following things
sudo as a user (johnsmith) and perform few things
Exit from that user and check url status
If status is not equal to 1 , ssh to one more server and execute a
script.
But when I am running it, the lines inside 'ENDBASH' are not getting executed at all.
#!/bin/ksh
echo "Outside ENDBASH ${###*/}"
sudo -u johnssmith bash <<'ENDBASH'
echo "Inside ENDBASH ${###*/}"
#Obtaining the new version file
for file in "${###*/}"
do
if echo "$file" | grep -E "abc_cde_efg"; then
echo "Version found: $file"
else
echo "Version not found"
fi
done
exit
ENDBASH
urlArray=('http://server:port/servicename1/services/servicename1?wsdl' 'http://server:port/servicename2/services/servicename2?wsdl')
status=0
for url in "${urlArray[#]}"
do
result=`curl -s $url`
if (echo $result | grep '<?xml' >/dev/null 2>&1); then
service=$(echo $url | cut -d"/" -f4)
echo "$service is Running"
else
service=$(echo $url | cut -d"/" -f4)
echo "$service is not Running"
status=1
fi
done
if [ $status != 1 ] ; then
ssh -t username#hostname /home/dev_was/test1.sh
fi
You need to explicitly pass the arguments received by your script to the internal script:
sudo -u johnssmith bash -s "$#" <<'ENDBASH'

DOS batch file - loop and increment by 1

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.

sql deploy with dos

How can I customize the the FOR command below to loop through the files inside the Database folder following the order of the number prefixed in the file name?
FOR /R ../Database %%f IN (*.sql) DO sqlcmd -S %1 -d %2 -U %4 -P %5 -i "%%~f" >> Logs/%2_DBInstall.log || goto errors
Database folder contains:
001_usp_procedure1.sql
002_ups_procedure2.sql
Thanks very much,
Please try the following
del temp.txt
del temp1.txt
for /F "usebackq " %%F IN (`dir /b /s /o:n "../Database/*.sql"`) DO (
echo %%~nF / %%F >> temp.txt
)
sort temp.text temp1.text
for /F "tokens=1,2 delims=/" %%i IN (temp1.txt) DO sqlcmd -S %1 -d %2 -U %4 -P %5 -i "%%j" >> Logs/%2_DBInstall.log || goto errors
It should work for recursive directories too (process is sorted by global filename without taking account of the path ... did I understand the problem?)
Please note that two temp files are being used. Be careful with the names (or use real temp files)