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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Photops
  • 312
  • 2
  • 6
  • 20

1 Answers1

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 "
kurin123
  • 363
  • 1
  • 6