Skip to main content

When we need to execute multiple scripts against a server it's cumbersome to open each file in Management Studio and execute them individually. To automate this we can use a command-line script which makes use of sqlcmd.exe - A command-line utility for SQL Server. We can create a batch file as below, it can be created as .bat or .cmd.

@echo off
setlocal

for /f %%i IN ('DIR *.sql /B') do call :RunScript %%i
goto :end

:RunScript
echo Executing script: %1
SQLCMD -S YourServer\YourInstance -E -i %1
echo Completed script: %1

endlocal
:end