Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run multiple SQL scripts using a batch file?

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?

like image 257
Willem Avatar asked Jan 13 '12 07:01

Willem


People also ask

How do I run a SQL script in a sequence?

If it's just "executed and there is no exception" - then just place all statements sequentially in stored procedure, and they will be executed sequentially. Computers are deterministic by design. If you execute these commands in a specific order, what makes you think they don't...


1 Answers

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.

like image 157
Galdur Avatar answered Sep 17 '22 12:09

Galdur