Executing DB Scripts using Batch File

This blog will provide the example to run the no. of scripts on sql server using bat file.

For Example:

If you have lot of functions, Views and Store procedure then you have to execute one by one or create one long file and then execute.Now here what you have to do place your script file in the folder with proper sequence

Like dependent schema, function ,view and store procedure in sequential order.

Folder Location

And then create a simple batch file and place the below code:

for /r %%G in (*.sql) DO (echo Executing: "%%G" >> output.txt
sqlcmd /S <<Server Name>> /d <<DB Name>> -U <<User name>> -P <<Password>> -i "%%G" >> output.txt
echo --------------------------------------------- >> output.txt
)
PAUSE

Note: Replace the highlighted section with your DB connection.

Bactch File Info

And click on RunAllSQLScripts if any error occours then it will write the error in output file.

Ebook Download
View all
Learn
View all