Follow by Email

Sunday, May 5, 2013

using bat file to Execute all sql scripts from a folder at once.

Suppose i have 1000- 2000 sql scripts in a folder which has to be executed in database.
executing single-single scripts manually can be very time consuming.

Instead i will create a batch file which will execute all the sql scripts in the database at once.

Batch file syntax:


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

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

Steps:
1. In the Notepad file add the batch file code as shown below:

For Example:

for %%G in (*.sql) do sqlcmd /S MILES124\SQLEXPRESS /d NORTHWND -E -i"%%G"
pause


2. Save this notepad filename as filename.bat in the sql scripts folder which we want to execute.



3. Double click on the .bat file to do the final execution