我对使用MSQL执行SQL没有任何了解。你必须弄清楚如何使用为MSQL提供的任何命令行实用程序来针对正确的数据库运行每个脚本。
我可以帮助你一个批处理文件,将以正确的顺序排序SQL文件,并解析出数据库的名称。
如果序号为零,前缀为固定宽度,则该作业在批处理中要简单得多。我假设可以重命名这些文件,所以这个解决方案就是这样做的。
我还假定你永远不会有超过999个文件来处理。代码可以很容易地修改以处理更多。
如果任何文件名包含!
字符,则必须进行一些更改,因为延迟扩展会破坏FOR变量的扩展。但这是一个不太可能的问题。
@echo off
setlocal enableDelayedExpansion
:: Change the definition to point to the folder that contains the scripts
set "folder=sqlCodeFolder"
:: The mask will only match the pattern that you indicated in your question
set "mask=[*] [*]*.sql"
:: Rename the .sql files so that the sequence numbers are zero prefixed
:: to width of 3. This enables the default alpha sort of the directory to be
:: in the proper sequence
for /f "tokens=1* delims=[]" %%A in ('dir /b "%folder%\%mask%"') do (
set seq=00%%A
ren "%folder%\[%%A]%%B" "[!seq:~-3!]%%B"
)
::Process the renamed files in order
for %%F in ("%folder%\%mask%") do (
for /f "tokens=2 delims=[] " %%D in ("%%~nF") do (
rem %%F contains the full path to the sql file
rem %%D contains the name of the database, without enclosing []
rem Replace the echo line below with the proper command to run your script
echo run %%F against database [%%D]
)
)
我假设这是Windows,因为你说“批处理文件”。如果您使用更好的脚本语言(例如通过Windows Scripting主机的VBscript或Javascript或PowerShell),则可以减少挫折时间。有了其中之一,它可能或多或少是微不足道的(读取文件名,解析它们并构建您的命令)。 – theglauber
我肯定会使用VBScript这一个。使用FileSystemObject获取脚本列表。 – Ben
什么是'MSQL'? 'MS SQL'或'MySQL'? –