K.A.D. K.A.D. - 1 year ago 64
SQL Question

Run all SQL files in a directory

I have a number of .sql files which I have to run in order to apply changes made by other developers on an SQL Server 2005 database.
The files are named according to the following pattern:

0001 - abc.sql
0002 - abcef.sql
0003 - abc.sql
...


Is there a way to run all of them in one go?

Answer Source

Another approach would be to create a .BAT file with the following command:

for %%G in (*.sql) do sqlcmd /S servername /d databaseName -E -i"%%G"
pause

Place this .BAT file in the directory from which you want the .SQL files to be executed, double click the .BAT file and you are done!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download