SQL Server: Execute SQL scripts in a folder
I have faced many situations where I had to execute more than a handful of SQL script on a SQL Server database. It can get monotonous and laborious at times. Here is a DOS command that uses SQLCMD to connect to a remote SQL Server and execute all the scripts in a given folder. If you’d like to execute the script files in a particular order, prefix the script file names with the sequence of execution. If the password has special characters use double quotes around the password to make it work.
FOR %%c in ("*.sql") DO sqlcmd -S [SERVERNAME] -d [DATABASENAME] -U [LOGIN] -P [PASSWORD] -i "./%%c"