需要帮助编写执行 sql 脚本的 bat 文件(sql server 2008 和另外 3 个文件.? [英] Need help to write bat file that execute sql scripts in (sql server 2008 and another 3 files.?

查看:27
本文介绍了需要帮助编写执行 sql 脚本的 bat 文件(sql server 2008 和另外 3 个文件.?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

执行位于不同文件夹中的sql脚本再执行3个bat文件.

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

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

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

我可以这样做吗?

:On Error exit 

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

我收到一个错误:

GO"不被识别为内部外部命令

"GO" is not recognized as internal external command

感谢您的任何意见

推荐答案

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

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.

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

Here are a couple examples all rolled into one. I'm using the DOS command START with the /WAIT switch, 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.

某些 ECHO 可能不是必需的,但脚本现在会回复您一点点.

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

@echo off

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

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.

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

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

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.

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

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

And this is how you can run a sql script, which is what the -i denotes, 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 -b will 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).

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

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

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.

哦!并记住 CTRL+C 会破坏正在处理的批处理脚本.

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

这篇关于需要帮助编写执行 sql 脚本的 bat 文件(sql server 2008 和另外 3 个文件.?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆