如何使用批处理文件运行多个SQL脚本?
我有一个案例,我有 10 多个 SQL 脚本
。
我不想一一运行我的所有脚本。
有没有一种方法可以让我在 SQL Management studio
中连续运行我的所有脚本。
我找到了这篇帖子。创建批处理文件似乎更容易。
这就是您所需要的:
@echo off
ECHO %USERNAME% started the batch process at %TIME% >output.txt
for %%f in (*.sql) do (
(
sqlcmd.exe -S servername -E -d databasename -i %%f >>output.txt
)
pause
替换服务器名和数据库名,但它似乎不起作用。
有什么想法吗?
I have a case where i have got 10+ SQL script
.
I don't want to go and run all my scripts 1 by 1.
Is there a way that i can run all my scripts in succession in SQL Management studio
.
I found this post. Creating a batch file seems easier.
This is all you need:
@echo off
ECHO %USERNAME% started the batch process at %TIME% >output.txt
for %%f in (*.sql) do (
(
sqlcmd.exe -S servername -E -d databasename -i %%f >>output.txt
)
pause
Replacing servername and databasename, but it seems to be not working.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
你有一个不匹配的括号。
尝试
for %%f in (*.sql) do sqlcmd.exe -S servername -E -d databasename -i %%f >>output.txt
我刚刚将其保存在 .cmd 文件中,它似乎正在工作。
You've got an unmatched parenthesis, there.
Try
for %%f in (*.sql) do sqlcmd.exe -S servername -E -d databasename -i %%f >>output.txt
I just saved it in a .cmd file and it appears to be working.
是的,这是可能的。您可以使用 SQLCMD 的 :r 命令来完成此操作。
我强烈建议您阅读本文并使用
SQLCMD
http://www.mssqltips.com/sqlservertip/1543/using-sqlcmd-to-execute-multiple-sql-server-scripts/
Yes, it's possible. You can do it with :r command of
SQLCMD
.I strongly recommend you to read this article and do it with
SQLCMD
http://www.mssqltips.com/sqlservertip/1543/using-sqlcmd-to-execute-multiple-sql-server-scripts/
您可以创建一个存储过程来调用所有脚本。您还可以创建一个计划来自动运行脚本。
http://msdn.microsoft.com/en -us/library/aa174792(v=sql.80).aspx
You can create a Strored Procedure to call all your Scripts. You could also create a schedule plan to run the scripts automaticaly.
http://msdn.microsoft.com/en-us/library/aa174792(v=sql.80).aspx
这是一个带有源代码的开源实用程序 http://scriptzrunner.codeplex.com/
该实用程序是用以下语言编写的c# 并允许您拖放许多 sql 文件并开始针对数据库运行它们。
Here is an open source utility with source code http://scriptzrunner.codeplex.com/
This utility was written in c# and allows you to drag and drop many sql files and start running them against a database.
您可以使用 SMSS 的 批处理编译器 插件,它可以让您一次运行多个脚本、创建 SQLCMD 脚本或将它们合并到*.sql 文件。
You can use Batch Compiler add-in for SMSS, it let's you run multiple scripts at once, create SQLCMD scripts or consolidate them into a *.sql file.
一些批量技巧
Some batch trick
如果您想通过批处理程序运行 Oracle SQL 文件,那么下面的代码将会很有用。只需复制&更改数据库凭据和数据库名称
基本上,您需要将此批处理文件放在包含所有 SQL 文件的文件夹中。它首先会获取目录中的所有sql文件名,并使用sql文件名加载它们的完整路径。然后,它将写入文件
all.sql
,然后sqlplus将调用该all.sql
来执行该目录中的所有sql文件。If you want to run Oracle SQL files through a Batch program, then the code below will be useful. Just copy & change the Database credential and DB names
Basically, you need to put this batch file in the folder where you have all the SQL files. It will first get all the sql file names in the directory and load their full path with the sql file names. Then, it will write into a file
all.sql
and then sqlplus will call thatall.sql
to execute all the sql files that you have in that directory.