SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Executing several SQL scripts from a bat file
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

bevans
Starting Member

10 Posts

Posted - 05/24/2012 :  05:09:56  Show Profile  Reply with Quote
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

United Kingdom
3560 Posts

Posted - 05/24/2012 :  05:37:36  Show Profile  Reply with Quote
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

Norway
3245 Posts

Posted - 05/24/2012 :  07:36:24  Show Profile  Reply with Quote
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

Edited by - Lumbago on 05/24/2012 07:37:07
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000