Question

I have a case where i have got 10+ SQL script.

I don't want to go and run all my scripts 1 by 1.

Is there a way that i can run all my scripts in succession in SQL Management studio.

I found this post. Creating a batch file seems easier.

This is all you need:

@echo off
ECHO %USERNAME% started the batch process at %TIME%  >output.txt


for %%f in (*.sql) do (
 (
sqlcmd.exe  -S servername -E   -d databasename -i %%f >>output.txt
)


pause

Replacing servername and databasename, but it seems to be not working.

Any ideas?

Was it helpful?

Solution

You can create a Strored Procedure to call all your Scripts. You could also create a schedule plan to run the scripts automaticaly.

http://msdn.microsoft.com/en-us/library/aa174792(v=sql.80).aspx

OTHER TIPS

You've got an unmatched parenthesis, there. Try

for %%f in (*.sql) do sqlcmd.exe -S servername -E -d databasename -i %%f >>output.txt

I just saved it in a .cmd file and it appears to be working.

Yes, it's possible. You can do it with :r command of SQLCMD.

I strongly recommend you to read this article and do it with SQLCMD

http://www.mssqltips.com/sqlservertip/1543/using-sqlcmd-to-execute-multiple-sql-server-scripts/

Here is an open source utility with source code http://scriptzrunner.codeplex.com/

This utility was written in c# and allows you to drag and drop many sql files and start running them against a database.

You can use Batch Compiler add-in for SMSS, it let's you run multiple scripts at once, create SQLCMD scripts or consolidate them into a *.sql file.

Some batch trick

cd %~dp0 //use this if you use 'for xxx in', it solved most of my problems 

ECHO %USERNAME% started the batch process at %TIME%  >output.txt


for %%f in (*.sql) do (
(
sqlcmd.exe  -S servername -E -d databasename -i %%f >>output.txt
)
echo %errorlevel%
pause
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top