1

I have to execute a bulk of scripts on sql server 2008r2.These scripts are present in many folders and subfolders.I created a batch file to execute these scripts but my problem is i have to put this bat file in a folder to execute the concerned folder's scripts.So, if there are more than 10 folders so i have to put it this bat file 10 times in a folder.Is there any way so that i can put my bat file just once wherever i want(like on desktop) and execute the folder's files?

.bat file:-

for %%G in (*.sql) do sqlcmd /S %1 /d %2 -E   -i"%%G" pause

Passing server name and database name using command prompt.

4

2 回答 2

1

假设您希望使用相同的服务器名和数据库名从多个目录执行 yourbatasposted,

@ECHO OFF
SETLOCAL
FOR /f "delims=" %%i IN (folderlist.txt) DO (
PUSHD "%%i"
CALL yourbatasposted %*
POPD
)

其中 folderlist.txt 是一个简单的文本文件,其中包含目标目录名称,一行一行。

如果您希望控制单独执行哪些服务器/数据库/目录,则创建 controllist,txt 格式为

servername 数据库目录

@ECHO OFF
SETLOCAL
FOR /f "tokens=1,2*delims=Q" %%i IN (controllist.txt) DO (
PUSHD "%%k"
CALL yourbatasposted %%i %%j
POPD
)

其中“Q”是 controllist.txt 中各列之间的分隔符。如果您选择使用空格,请省略 delims= 子句。

当然,yourbatchasposted.bat 应该位于 %path% 变量中提到的任何目录中...

于 2013-03-10T01:21:53.060 回答
1

/R选项添加FOR到递归搜索:

FOR /R . %%G IN (*.sql) DO ... etc ...

这将在当前目录 ( ) 下的任何位置搜索 *.sql 文件.。或者,FOR /R C:\Folder %%G IN (*.sql) DO ...如果您想要一个特定的文件夹作为根目录,请创建它。

于 2013-03-09T18:01:12.993 回答