I have a bunch of scripts in a folder which I have to run in order. I just open it up in SSMS and hit execute one by one. I have to wait for one of them to complete then run the second one.
Is there a way for me to create a batch script or a script in SSMS (if its possible) that will go through the files one by one? This would save me time as I can do something else while the scripts are running and prevent error (most of them rely on temp tables created in succession).
I think a batch script with sqlcmd is what I am looking for but no idea how to accomplish this task.
Thank you.
Very easy to put together a batch file for this. Put something like the following into a text file but end the file with .bat, eg 'ExecuteMyScripts.bat'
Sqlcmd takes various arguments but the main ones are -S for server instance, -d for database, -i for input file ie your SQL file, -U for user, -P for password, and -o for output file. Watch the case of the letters.
So open notepad and add the following:
sqlcmd -S mysqlserver -d mydb -U sa -P pass1 -i "c:\script1.sql" -o "c:\script1log.txt"
IF ERRORLEVEL 1 goto :eof
sqlcmd -S mysqlserver -d mydb -U sa -P pass1 -i "c:\script2.sql" -o "c:\script2log.txt"
IF ERRORLEVEL 1 goto :eof
sqlcmd -S mysqlserver -d mydb -U sa -P pass1 -i "c:\scripr3.sql" -o "c:\script3log.txt"
Save the file as ExecuteMyScripts.bat.
Double click the file and both script will be executed.
Adding 'IF ERRORLEVEL 1 goto :eof' after each sqlcmd call will jump to the end of the script if error found.
Full usage found here, http://msdn.microsoft.com/en-us/library/ms162773.aspx
Related
I have created a bat file named test.bat as below :
sqlcmd -v item="%item%" -v cost=%cost% -i test.sql sqlcmd -S "172.16.253.72,17001" -U user -P pdw -d Master -I C:\Maintenance\test_blocking_backup\test.sql -o C:\Maintenance\test_blocking_backup\output.txt
so when i will run this file it should prompt me for the value of item and cost. but then how can i fetch the value entered by the user in a variable
Using .Bat file with input Parameters
1) With Prompt:
add before your sqlcmd line
echo off
set /p item=Enter item:
set /p cost=Enter cost:
2) without Prompt :
add before your sqlcmd line
set item=%1
set cost=%2
Then call your bat file like this
test.bat itemvalue,costvalue
where itemvalue and costvalue are the parameters values to pass to the .bat file
I'm using a batch file to call sequential SQL Agent jobs. That part works fine, but my current challenge is that I need to stop the process when one of the SQL Agent jobs fails.
Business Requirement:
Step 1: Call SQLAgentJob1 to run on DBServer. If SQLAgentJob1 fails, this batch script should exit (not call other steps). If SQLAgentJob1 succeeds, call SQLAgentJob2.
Step 2: Call SQLAgentJob2 to run on DBServer. If SQLAgentJob2 fails, this batch script should exit (not call other steps). If SQLAgentJob2 succeeds, call the RunMe.cmd.
Step 3: Call RunMe.cmd to run on WebServer
Step 4: Call SQLAgentJob3 to run on DBServer. If SQLAgentJob3 fails, this batch script should exit. If SQLAgentJob3 succeeds, this batch script should exit.
Current Status
I have used the batch file to successfully call each of the SQL Agent jobs and the command line task, but I'm stuck on trying to STOP the process if any one of the SQLAgentJobs fail. Right now, even if SQLAgentJob1 fails, the batch file still calls SQLAgentJob2.
Here is the scripting as it currently stands:
ECHO starting SQLAgentJob1
sqlcmd -S DBServer -E -d MSDB -Q "sp_start_job 'SQLAgentJob1'"
ECHO starting the SQL job
sqlcmd -S DBServer -E -d MSDB -Q "sp_start_job 'SQLAgentJob2'"
ECHO OFF
ECHO.
set CMDDIR = D:\webapps
pushd CMDDIR
call RunMe.cmd
popd
ECHO starting SQLAgentJob1
sqlcmd -S DBServer -E -d MSDB -Q "sp_start_job 'SQLAgentJob3'"
Also, very challengingly, even when the SQL Agent Job fails, the %errorlevel% that is returned as part of the batch file for that job is 0 (not 1).
Any ideas/suggestions? Ideal world I'd like to continue using the batch scripting (in part because SQL isn't installed on my webserver, and I need to call the .cmd from my webserver and also run the SQL Agent jobs), although if it's impossible or too difficult, I can change to something else.
Thanks in advance for any help and forgive me if I've used any terminology incorrectly.
I found with a simple search Return value of SQLCMD.
The problem should be (not tested) easily solved using -b parameter on sqlcmd.
ECHO starting SQLAgentJob1
sqlcmd -S DBServer -E -d MSDB -b -Q "sp_start_job 'SQLAgentJob1'"
if errolevel 1 exit /b
ECHO starting the SQL job
sqlcmd -S DBServer -E -d MSDB -b -Q "sp_start_job 'SQLAgentJob2'"
if errorlevel 1 goto :EOF
ECHO OFF
ECHO.
set CMDDIR = D:\webapps
pushd CMDDIR
call RunMe.cmd
popd
ECHO starting SQLAgentJob1
sqlcmd -S DBServer -E -d MSDB -Q "sp_start_job 'SQLAgentJob3'"
Or you use -V X option additionally to -b to exit the batch file if an error of level X specified on usage of -V occurs.
ECHO starting SQLAgentJob1
sqlcmd -S DBServer -E -d MSDB -V 11 -b -Q "sp_start_job 'SQLAgentJob1'"
if errolevel 1 exit /b
ECHO starting the SQL job
sqlcmd -S DBServer -E -d MSDB -V 11 -b -Q "sp_start_job 'SQLAgentJob2'"
if errolevel 1 goto :EOF
ECHO OFF
ECHO.
set CMDDIR = D:\webapps
pushd CMDDIR
call RunMe.cmd
popd
ECHO starting SQLAgentJob1
sqlcmd -S DBServer -E -d MSDB -Q "sp_start_job 'SQLAgentJob3'"
Please let us know what works best and I will edit my answer accordingly.
I'm in a process of creating a batch file to list names of all SQL scripts in a folder to a text file. My plan to convert this text file to a batch file so that I can use it to execute the scripts in the server. So I would like to have the following string appended before each file name while creating the initial text file
sqlcmd -S %MSSQLSERVER_NAME% -d %MSSQLSERVER_DBNAME% -i
This is a batch file command and I would like to be appended before the each file names.
eg:
sqlcmd -S %MSSQLSERVER_NAME% -d %MSSQLSERVER_DBNAME% -i 001_ALTER_PERSON.sql
The code I'm using is
set MSSQLSERVER_NAME = "%MSSQLSERVER_NAME%"
set %MSSQLSERVER_DBNAME = "%MSSQLSERVER_DBNAME%"
set myvar=sqlcmd -S %MSSQLSERVER_NAME% -d %MSSQLSERVER_DBNAME% -i
for /r . %%g in (*.sql) do echo %myvar% %%~nxg >> D:\test.txt
pause
Out put I'm getting
sqlcmd -S -d -i 015_ALTER_vBOARD_PAPERS.sql
Let me know how to tackle this
set myvar=sqlcmd -S %%MSSQLSERVER_NAME%% -d %%MSSQLSERVER_DBNAME%% -i
should cure your problem - % escapes %
note that spaces ARE significant in variable names, so set var = somethingwill set var[space] to [space]something
set %varname%=... is rare -it sets the variablename (contents of varname).
Even more rare to have unbalanced %... not sure that will work at all...
I would like to be able to shrink all log files in nested folders. I have used this same script to modify databases but I am having problems with the shrink command.
When I run this script, I get an error stating it can not find the database in the sys.database_files. So I'm guessing my database is going by a different name in sys.database_files or simply is not added. Can you help?
for /r /d %%i in (*) do (
ECHO %%i\MY_DB.mdf
osql -S LOCALHOST\SQLEXPRESS -U johan -P johan_j-d master -Q "EXEC sp_attach_db 'MY_DB', '%%i\MY_DB.mdf', '%%i\MY_DB_log.ldf';"
osql -S LOCALHOST\SQLEXPRESS -U johan -P johan_j -d MY_DB -Q "DBCC SHRINKFILE (N'MY_DB_log',0,TRUNCATEONLY);"
osql -S LOCALHOST\SQLEXPRESS -U johan -P johan_j -d master -Q "EXEC sp_detach_db 'MY_DB';"
)
Problem solved. I should have been using the logical file name not the physical file name.
I am trying to restore database data using the following command from a large .sql file
Type myfile.sql | sqlsmd –S server –U username –P password
I get the following error:
Sqlcmd: Error: Scripting error.
I am unable to open the file, there is not enough memory.
SQLCMD has a "-i" inputfile parameter as well - I'd recommend using that. "Type"-ing the file and piping your output into SQLCMD is a hackish way to run the file, especially when the command line includes native support for reading from a file. Try this:
sqlcmd –S server –U username –P password -i MyFile.sql
you could try using isql which has a -i input file option - that loses the nasty
Type myfile.sql |
You don't say which version of Sql you're using, but you could probably run your file in using DTS/SSIS, if you go with that approach you'll be able to turn on logging and actually see which line(s) fail, and set a threshold for an acceptable number of failing lines.
Another way is to use management studio to open the file and then execute it.
Try this
#echo Server: %1
#echo Database: %2
#echo User: %3
#echo Password: %4
set ISQLCMD=SQLCMD -S %1 -d %2 -U %3 -P %4 -m1 -i