0

I want to run 20 SQL scripts from a folder on a database in SQL Server.

My SQL Server is called SQL_SERVER_1, my database is called SQL_DATABASE_1.

I have 20 SQL files (01 - file, 02 - file, 03 - file, etc) in a folder c:\Users\Me\Desktop\New Folder.

I'm unable to get the syntax correct for this.

On a separate note: is there any way I run all the files inside the folder consecutively one after the other, without having to write the command for every file?

2
  • Look at the FOR command on the command line to run them all in a single command: stackoverflow.com/q/24316254/2557263 Commented Apr 20, 2016 at 15:56
  • what error messages are you getting? Commented Apr 20, 2016 at 16:01

1 Answer 1

2

Sure you can:

Locate SQLCMD.EXE, depends on version of SQL server

"C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.EXE" -U <UserName> -P <Password> -S (local)\SQLExpress -i "c:\Users\Me\Desktop\New Folder\01 - file.sql"

To run it for all files check ForFiles.

forfiles -M *.sql -C "cmd /c <your path to SQLCMD>SQLCMD.EXE <your connection parameters> @Path "
Sign up to request clarification or add additional context in comments.

Comments

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.