3

I have around 100 .sql files that create stored procs. I'd like to execute them at once all of them.

They are contained inside a folder called Stored Procedures. There several other folders with different names --Table names-- inside the Stored Procedures folder.

Is there a way to execute all these files at once?

3 Answers 3

3

You could open up a PowerShell instance and run a command like the following:

Get-ChildItem ".\Stored Procedures\*.sql" | ForEach-Object { sqlcmd -S ServerName -d DatabaseName -E -i $_.FullName }

This will get each .sql file in the Stored Procedures directory, and one by one, pass it to the sqlcmd.exe program. To get more details on the parameters for sqlcmd you can run sqlcmd /?.

To make it recursive through sub directories, you can apply the -Recurse parameter to Get-ChildItem along with the -Filter parameter:

Get-ChildItem '.\Stored Procedures' -Filter *.sql -Recurse | ForEach-Object { sqlcmd -S ServerName -d DatabaseName -E -i $_.FullName }
Sign up to request clarification or add additional context in comments.

4 Comments

Is there a way to make it recursive? I mean, to get all sql scripts that insede folders inside Stored Procedures folder.
@tou - to recurse you can add the -Recurse parameter to the Get-ChildItem cmdlet. I'll update the answer.
Thats great, but how can i be sure that the scripts are executed in the right order (tables first, views second... functions, procedures, triggers and so on)?
@Ice - I would recommend putting the different script types in different folders and running across the above command across the folders sequentially. This would mean, placing table scripts in a Table folder, placing view scripts in a Views folder, etc. I've actually done this and is how I can create a database from scratch. If need be I can update the answer with a more detailed example.
2

Here is the another tool Sql_Server_Script_Executor

Is there a way to execute all these files at once?

*You can add folder and all your files will comes up in the list. Click the execute button and done!!!!!!!!!!!!! *

It contains three transaction modes.

1. Execute scripts within one transaction
2. Execute scripts on separate transaction
3. No transaction

and many more features please go through it.

enter image description here

1 Comment

I ended up using this. It even has a feature to create a batch command, so you can run it from the command line.
1

Not at once - but all in one go - try a tool like the SSW Deploy one.

enter image description here

1 Comment

@tou: you didn't require free - and good software does cost - but it's worth its price - SQL Deploy **is"" worth its price - many times over :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.