ANP
ANP

Reputation: 15607

Executing sql files automatically

Suppose I am having 100 sql files and I need to execute all the files one by one in sequence. Is there any approach to do this with out executing the scripts manually?

Upvotes: 1

Views: 2516

Answers (6)

Waleed Al-Balooshi
Waleed Al-Balooshi

Reputation: 6406

You can use PowerShell to do this. The following blog post describes such a script. As part of the foreach a pipe is used to sort the files in the manner that you want to process them. In this example it is being sorted by descending alphabetical file name, but you can also do it by other attributes, such as the date the file was created.

Also the following blog post describes how to run all the .sql files in a directory like the above linked post, but without the use of PowerShell

Upvotes: 1

ObiWanKenobi
ObiWanKenobi

Reputation: 14892

Assuming your files are named something like this:

001_my_script.sql
002_another_script.sql
003_foo_script.sql
004_bar_script.sql

You can do the following at the command line:

copy *.sql /a my_big_script.sql

And then run the resulting file as one script (via sqlcmd or Management Studio).

Upvotes: 0

gbn
gbn

Reputation: 432311

  • Pipe the dir /b > foo.txt output to a file
  • Add sqlcmd at the start of each line etc using a decent text editor like notepad++

Upvotes: 1

marc_s
marc_s

Reputation: 754598

We've had great success with the SQL Deploy tool by SSW Australia.

alt text

It's not free - but worth every penny, and saves you so much time, it pays for itself in no time at all!

(I have no affiliation with SSW Australia other than being a happy user of SQL Deploy)

Upvotes: 1

Giorgi
Giorgi

Reputation: 30883

You can write a bat file to execute them using sqlcmd Utility

Upvotes: 3

adarshr
adarshr

Reputation: 62593

Write a shell script or similar to run them sequentially.

Upvotes: 1

Related Questions