Caveman's Blog

My commitment to learning.

SQL Server: Execute SQL scripts in a folder

leave a comment »


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"
Advertisements

Written by cavemansblog

December 3, 2010 at 10:13 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: