OSQL - Running all the SQL Scripts in a given folder

Like I said in my earlier post OSQL is very useful to run sql scripts and run queries.This post demonstrates the use of OSQL in running all the scripts in a given folder.

@ECHO OFF
:: Keep variables local

SETLOCAL
:: Check command line arguments
SET Server=%1
SET Database=%2
SET Test=%3

IF NOT DEFINED Server
GOTO Syntax

IF NOT DEFINED Database GOTO Syntax

IF DEFINED Test GOTO Syntax

:: Execute sql scriptsFOR %%f IN (*.sql)
DO
(
:: ECHO Running script %%f... ECHO ^>^> %%f
OSQL -E -S %1 -d %2 -n -m-1 -b -i "%%f"
IF ERRORLEVEL 1
( ECHO.
ECHO ^>^> Critcal error in script %%f! Ending execution.
GOTO End
)
)
:: Display complete message
ECHO.
ECHO Finished running sql scripts on database %2

:: Done
GOTO End

:Syntax
ECHO.
ECHO Usage: RunSQLScripts server database scriptpath
GOTO End

:End
ENDLOCAL

Comments

Popular posts from this blog

Duplicate Folders Freeware - Not Duplicate Files

SSRS Compile Errors

Free SQL Server Management Studio Addin/Plugin/External Tool to get object information quickly