Running all scripts in a directory against a particular database

At some point, you may have the need to rapidly apply a bunch of scripts on a particular database.  Rather than manually opening each in Management Studio and executing them, you can use sqlcmd to run all scripts in a particular directory, in alphabetical order.  Should save you some time:

for %f in (“c:\path\to\dir\*.sql”) do sqlcmd -S [SERVER_NAME] -d [DATABASE_NAME] -i “%f” -b

Tagged , , ,

4 thoughts on “Running all scripts in a directory against a particular database

  1. JB says:

    Great! Just what I needed today. Thanks

  2. Anonymous says:

    That’s what I need. But where do I run that code?

  3. Treena Stoia says:

    Hi there! I am so grateful that I found your blog. I really admire the dedication you put into presenting the detailed information for free. I will definitely come back to your blog for more. Keep up the good work. By the way, this system helps me making around $650 per day, if you allow me, I suggest you to take a look at this secret offer page to access your very own turnkey software business to increase your online income. Wish you more success, all the best!

Leave a comment