Photops
Photops

Reputation: 322

How to run SQL scripts from command prompt directly

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?

Upvotes: 0

Views: 3585

Answers (1)

kurin123
kurin123

Reputation: 363

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 "

Upvotes: 2

Related Questions