运行 多个目录中的所有 SQL 个文件

Run all SQL files in multiple directories

我结合了在网上找到的几个解决方案来尝试实现这一目标。

https://sqlandme.com/2013/03/25/sql-server-executing-multiple-script-files-using-sqlcmd/

我正在尝试 运行 单个 .cmd 脚本 (Script1.cmd),其文件夹位置为 .sql 文件。该单个脚本 运行 是另一个脚本 (Script2.cmd) 使用 sqlcmd 执行该文件夹位置中的所有 .sql 文件。它主要工作,但它留下了一个命令 window 打开,我必须从每个文件夹位置退出。

Script1.cmd

start Script2.cmd "C:\Location1"
start Script2.cmd "C:\Location2"

Script2.cmd

@Echo Off
FOR /f %%i IN ('DIR %1\*.Sql /B') do call :RunScript %1 %%i
GOTO :END

:RunScript
Echo Executing Script: %2
cd %1
SQLCMD -S Server123 -d Database456 -E -i %2
Echo Completed Script: %2

:END

Windows XP or for Windows Server 2003, Windows Vista (and above) seems to be too brief. Read this (extended) start command documentation 的官方命令行参考:

语法:START "title" [/D path] [options] "command" [parameters]

Always include a TITLE this can be a simple string like "My Script" or just a pair of empty quotes "". According to the Microsoft documentation, the title is optional, but depending on the other options chosen you can have problems if it is omitted.

If command is an internal cmd command or a batch file then the command processor is run with the /K switch to cmd.exe. This means that the window will remain after the command has been run.

下一个 Script1.cmd 应该工作并关闭启动的命令 windows:

start "" cmd /C Script2.cmd "C:\Location1"
start "" cmd /C Script2.cmd "C:\Location2"