Tuesday, July 24, 2012

Concatenate multiple SQL files into one big SQL file

Assuming your file naming pattern is like this:

  • 001_do_first_step.sql
  • 002_do_second_step.sql
  • 003_do_other_stuff.sql

You can concatenate them into one big, valid sql file by saving the following as a .bat file and double-clicking it:

@ECHO OFF
del allscripts.sql
for %%f in (0*.sql) do (
  echo. >> allscripts.sql
  echo ------------------- >> allscripts.sql
  echo -- Begin File: %%f >> allscripts.sql
  echo ------------------- >> allscripts.sql
  echo. >> allscripts.sql
  type %%f >> allscripts.sql
  echo. >> allscripts.sql
  echo GO >> allscripts.sql
  echo. >> allscripts.sql
  echo ------------------- >> allscripts.sql
  echo -- End File: %%f >> allscripts.sql
  echo ------------------- >> allscripts.sql
  echo. >> allscripts.sql
  echo. >> allscripts.sql
)
rem start allscripts.sql
echo allscripts.sql has been (re)created.
echo.
echo Hit enter to continue
pause