需要帮助编写执行 sql 脚本的 bat 文件(sql server 2008 和另外 3 个文件。?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3794897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 07:40:58  来源:igfitidea点击:

Need help to write bat file that execute sql scripts in (sql server 2008 and another 3 files.?

sqlsql-servertsqlsqlcmd

提问by user451259

I am sure these has been asked before but cannot find clear instruction how to create a batch file lets call it "Update Database" this batch file should

我确信之前已经问过这些问题,但找不到明确的说明如何创建批处理文件让我们称之为“更新数据库”这个批处理文件应该

Execute sql scripts located in different folders Execute another 3 bat files.

执行位于不同文件夹中的 sql 脚本 执行另外 3 个 bat 文件。

Any quick examples how to do it?Never done it before thanks a lot

任何快速示例如何做到这一点? 以前从未做过,非常感谢

EDITED

已编辑

Can I do this?

我可以这样做吗?

:On Error exit 

:r C:\myPath\MasterUpdateDatabase.bat
GO 
SQLCMD -S (Local) -i C:\myPath\InsertUsername.sql

I get an error:

我收到一个错误:

"GO" is not recognized as internal external command

“GO”不被识别为内部外部命令

Thanks for any input

感谢您的任何意见

采纳答案by Vinnie

It looks like you're trying to use DOS commands to create a batch file that either (a) executes other batch files or (b) executes SQLCMD to run sql or a sql script.

看起来您正在尝试使用 DOS 命令来创建一个批处理文件,该文件要么 (a) 执行其他批处理文件,要么 (b) 执行 SQLCMD 以运行 sql 或 sql 脚本。

Here are a couple examples all rolled into one. I'm using the DOS command STARTwith the /WAITswitch, which will keep your original "master" batch file running in one window and execute the subsequent file or commands in a new window. That new window stays open until the script finished AND exits.

这里有几个例子全部合二为一。我正在使用START带有/WAIT开关的 DOS 命令,这将使您的原始“主”批处理文件在一个窗口中运行,并在新窗口中执行后续文件或命令。该新窗口保持打开状态,直到脚本完成并退出。

Some of the ECHOs probably aren't required, but the script will talk back to you for now, a little.

有些ECHOs 可能不是必需的,但脚本现在会回复您一点。

@echo off

So, this is pretty simple in the sense that you're just running the script. If you're script1.bat has break points, you can return an error back to the main script and have it end immediately. I wasn't clear if that was what you needed the master script to do.

所以,这很简单,因为您只是在运行脚本。如果您的 script1.bat 有断点,您可以将错误返回给主脚本并立即结束。我不清楚这是否是您需要主脚本执行的操作。

echo Starting Database Update.
echo.

echo Excuting Script 1
echo.
start /wait C:\path\to\your\script1.bat

echo If there was a problem, break here.
Pause

echo Excuting Script 2
echo.
start /wait C:\path\to\your\script2.bat

echo If there was a problem, break here.
Pause

Here is where did used the same START /WAIT to run SQLCMD, which in this case just returns results from the query. One thing to note here is that the -Q(uppercase) runs the query and quits. If you use -q(lowercase) it will run the query and sit open in SQLCMD waiting for another query.

这里是使用相同的 START /WAIT 来运行 SQLCMD 的地方,在这种情况下它只返回查询的结果。这里要注意的一件事是-Q(大写)运行查询并退出。如果您使用-q(小写),它将运行查询并在 SQLCMD 中打开等待另一个查询。

echo.
echo Running SQLCMD: "select top 100 * from sys.objects"
start /wait sqlcmd -S (local) -Q "select top 100 * from sys.objects"

And this is how you can run a sql script, which is what the -idenotes, but I also didn't run this in the START /WAIT as earlier. Not that you have to, but I wanted to show both examples. What this also shows is the -bwill end the batch process if your script returns an error, which is useful if you're running multiple scripts that depend on success of the former(s).

这就是您可以运行 sql 脚本的方式,这就是-i表示的内容,但我也没有像之前那样在 START /WAIT 中运行它。不是你必须这样做,但我想展示这两个例子。这还表明,-b如果您的脚本返回错误,将结束批处理,如果您正在运行依赖于前者成功的多个脚本,这将非常有用。

echo.
echo Running SQLCMD from an (-i)nput file:
sqlcmd -S (local) -i  C:\path\to\your\script.sql -b

echo.
echo Update Complete.
pause

End

So, I assumed you were looking for a .bat or .cmd file that utilized SQLCMD. The example I provided is pretty basic, but hopefully it sets you on the right path.

因此,我假设您正在寻找使用 SQLCMD 的 .bat 或 .cmd 文件。我提供的示例非常基本,但希望它能让您走上正确的道路。

OH! And remember that CTRL+C breaks a batch script in process.

哦!请记住,CTRL+C 会破坏正在处理的批处理脚本。

回答by bneal

The actual error you're seeing is that the command line interpreter does not recognize 'GO', so you could just remove that line.

您看到的实际错误是命令行解释器无法识别“GO”,因此您可以删除该行。

回答by Shessuky

Hope this helps you :

希望这可以帮助您:

sqlplus UserName/Password@DataBase @C:\myPath\InsertUsername.sql

P.S : Don't forget to add the command "commit;" at the end of sql file (InsertUsername.sql), this command order Oracle to save performed changes in darabase

PS:不要忘记添加命令“commit;” 在 sql 文件 (InsertUsername.sql) 的末尾,此命令命令 Oracle 在 darabase 中保存已执行的更改

回答by user3215454

This answer definitely works for your purposes:

这个答案绝对适用于您的目的:

sqlcmd -S localhost -U fdmsusr -P fdmsamho -i "E:\brantst\BranchAtt.sql" -o "E:\brantst\branchlog.txt"

sqlcmd -S localhost -U fdmsusr -P fdmsamho -i "E:\brantst\BranchAtt.sql" -o "E:\brantst\branchlog.txt"