Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Executing several SQL scripts from a bat file

Author  Topic 

bevans
Starting Member

10 Posts

Posted - 2012-05-24 : 05:09:56
Hi,
I've been struggling with this issue, any help would be really appreciated.

I have a folder that has many different SQL scripts, about 20 of them.
The scripts need to be run in order. So script one needs to run first, script two runs second etc..

These scripts run against 4 different databases. So script one may run against database 1, and script two also against database 1, but script 3 runs against database 3 etc.

Is there a way to create a bat file that automatically runs all these scripts, in order against, the databases they need to?
The databases that they need to run against have the name of the database at the beginning of the name of the file.

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2012-05-24 : 05:37:36
You could easily create a batch file with osql commands to run these, or you could have linked servers and a SQL job on one of the servers that would do the same thing.
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2012-05-24 : 07:36:24
Are all the databases on the same server or are the servers different as well? If they are all on the same server you should put a "USE databasename" inside each .sql-file to make sure that the scripts are executed in the right one.

Here is .bat-script you can use to run all .sql-files in a folder using sqlcmd:


@SET scriptdir=c:\scripts
@FOR /f %%G in ('dir /b %scriptdir%*.sql ') DO @sqlcmd -S myServername -i %scriptdir%%%G



- Lumbago
My blog-> http://thefirstsql.com
Go to Top of Page
   

- Advertisement -