Friday, March 23, 2012

How to run multiple script files

I have a number of script files that create various objects in my database, and to create a new DB from scratch I need to run them all in a certain sequence.

Is it possible to create a master script file that calls the others in the desired sequence?

P.S. I am using SQL Server Management Studio Express to edit and run the scripts.

There is no need to separate all your objects into separate script files, you can include them all in a single script and just separate them into Batches as appropriate. This is how we create our sample database in many cases and how most people deploy databases as part of an application.

Mike

|||

Thanks for the advice, but I use a code generator and therefore need to have them in separate files.

Anyway, I worked out how to do it. First you have to click the Query menu and choose SQLCMD Mode. Then you can enter the files like this:

r: c:\myfiles\file1.sql
r: c:\myfiles\file2.sql
etc...

My path is quite long so I use a variable as follows (I also added the server connection):

:Connect MyServer\SQLExpress
:setvar SourceFolder "K:\Product Development\Programming\Current\Database\Stored Procedures"
:r $(SourceFolder)\SProc1.sql
:r $(SourceFolder)\SProc2.sql

No comments:

Post a Comment