Home > Management Studio, SQLServer, SQLServer 2005, SQLServer 2008, SQLServer 2008 R2, SQLServer 2012, Undocumented Functions > SQL Server – Executing Multiple Script Files Using SQLCMD

SQL Server – Executing Multiple Script Files Using SQLCMD

There are different options available when you want to execute a .sql script file on a server. You can open the script file in SQL Server Management Studio and execute it, or you can use SQLCMD to execute a script from command line. However, when you have large number of scripts to execute these methods may not be feasible.

You can use SQLCMD and batch programming together to execute a large number of script files easily.

Below is the code for batch file:

@Echo Off

FOR /f %%i IN ('DIR *.Sql /B') do call :RunScript %%i




Echo Executing %1

SQLCMD -S Server\Instance -U Vishal -P Password -i %1

Echo Completed %1



Copy this to notepad and save it as a .cmd or .bat file, for example save it as "RunMyScripts.cmd" in the same folder where your scripts are stored.

Make sure you replace server details and authentication details.

Then go to command prompt and navigate to this folder and execute “RunMyScripts.cmd”

Hope This Helps!


If you like this post, do like my Facebook Page -> SqlAndMe
EMail me your questions -> Vishal@SqlAndMe.com
Follow me on Twitter -> @SqlAndMe

  1. ram
    August 28, 2013 at 11:40 am

    I Have number of.sql files at a time i want to execute all the files .Please send the me script for that

  2. Doug Ivison
    March 30, 2014 at 6:09 pm

    To execute multiple sql scripts on one command line, separate the script names with commas. This example uses windows authentication (-E), and executes scripts in the current directory: SQLCMD -S Server\Instance -d DatabaseName -E -i script1.sql,script2.sql,script3.sql

  3. Steven B
    February 14, 2017 at 2:21 am


    i am trying to use this script, however i am getting an error message saying “The system cannot find the path specified.”, I have everything exactly what it says to replace (, directory of the scripts, server name, username and password, here it is below, maybe i am doing something wrong in my .cmd file?

    @Echo Off
    FOR /f %%i IN (‘\\serverB\Test\Results\*.Sql /B’) do call :RunScript %%i

    Echo Executing %1
    SQLCMD -S ServerB\MSSQLSERVER -U Test -P Test -i %1
    Echo Completed %1


  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: