1

A SQL Server database that is in development has a Project folder, and within it there are subfolders Tables, Views, Triggers, Sprocs. Within each subfolder are several .sql files and other miscellaneous folders.

The goal is to create a batch file which executes all .sql files within the subfolders, making use of SQLCMD for the execution.

How can a batch file be written to look in the Project's subfolders to find *.sql so they're executed in the database automatically?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
knot22
  • 2,648
  • 5
  • 31
  • 51
  • possible duplicate of [Run all SQL files in a directory](http://stackoverflow.com/questions/2583517/run-all-sql-files-in-a-directory) , Just make sure you execute the files in `Tables` folder 1st and then `Views` , `Procs` and at last `Triggers`. – M.Ali Aug 09 '14 at 22:06
  • I had found that thread prior to posting - it does not say how to iterate through specific folders. – knot22 Aug 09 '14 at 23:20
  • 1
    Can you not write this command 4 times passing the folder names in specific order, as the order in which these .sql files are executed is really important. – M.Ali Aug 09 '14 at 23:23
  • 1
    All you need is the FOR /R option. But like M Ali has stated, the order in which they run is probably critical. The files will be processed in alphabetical order within each folder, and folders are processed alphabetically depth first. This may or may not be appropriate. – dbenham Aug 11 '14 at 01:41
  • Quite right - execution order is very important and I had not thought about that. Therefore, each file will need to be listed after all in the batch file. Thank-you both, M.Ali and dbenham, for pointing out this critical fact. – knot22 Aug 11 '14 at 02:26

0 Answers0