Search code examples
sqlsql-serversql-server-2008deploymentsqlcmd

Running SQL script script/s on multiple databases at the same time (Parallel execution)


I currently use sqlcmd to execute scripts on a database within my deployment process. Now with multiple database one solution is to use this very script in a batch or foreach database. The problem with this, it applies the script to the database one at a time. I need something that can save time.

Can SQLCMD execute a script on multiple database in Parallel? If not, are there a script command I can leverage to accomplish this?

There are similar questions, but none with the right solution or the stress that it has to be a method to execute in Parallel which is the main goal. The tool SQL Multi Script does exactly this, but its not open source plus I prefer scripting it out.

Any ideas? Thanks in advance.


Solution

  • I was able to accomplish this using the following.

    1. The user will set which databases to deploy to in a specific instance.
    2. Foreach database it will generate a set of SQLCMD commands foreach script to deploy and place this in a batch file.

    For Example: SQLCMD_Commands_DB1.bat and SQLCMD_Commands_DB2.bat

    1. Foreach batch file created with will then generate a new master batch file with the START commands.

      START "" "SQLCMD_Commands_DB1.bat" START "" "SQLCMD_Commands_DB2.bat"

    2. Execute the Master batch file which will deploy the database scripts in Parallel. I written this and it works as expected.