如何使用批处理文件运行多个 SQL 脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8847233/
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
How to run multiple SQL scripts using a batch file?
提问by Willem
I have a case where i have got 10+ SQL script
.
我有一个案例,我有 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
.
有没有一种方法可以让我在SQL Management studio
.
I found thispost. 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.
替换 servername 和 databasename,但它似乎不起作用。
Any ideas?
有任何想法吗?
采纳答案by Andreas Rohde
You can create a Strored Procedure to call all your Scripts. You could also create a schedule plan to run the scripts automaticaly.
您可以创建一个 Strered 过程来调用您的所有脚本。您还可以创建一个调度计划来自动运行脚本。
http://msdn.microsoft.com/en-us/library/aa174792(v=sql.80).aspx
http://msdn.microsoft.com/en-us/library/aa174792(v=sql.80).aspx
回答by Galdur
You've got an unmatched parenthesis, there. Try
你有一个无与伦比的括号,在那里。尝试
for %%f in (*.sql) do sqlcmd.exe -S servername -E -d databasename -i %%f >>output.txt
对于 (*.sql) 中的 %%f 执行 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.
我只是将它保存在一个 .cmd 文件中,它似乎正在工作。
回答by Chuck Norris
Yes, it's possible. You can do it with :r command of SQLCMD
.
是的,这是可能的。您可以使用 :r 命令来完成SQLCMD
。
I strongly recommend you to read this article and do it with SQLCMD
我强烈建议您阅读这篇文章并使用 SQLCMD
http://www.mssqltips.com/sqlservertip/1543/using-sqlcmd-to-execute-multiple-sql-server-scripts/
http://www.mssqltips.com/sqlservertip/1543/using-sqlcmd-to-execute-multiple-sql-server-scripts/
回答by Clinton Ward
Here is an open source utility with source code http://scriptzrunner.codeplex.com/
这是一个带有源代码的开源实用程序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.
该实用程序是用 c# 编写的,允许您拖放许多 sql 文件并开始针对数据库运行它们。
回答by push_ebp
Some batch trick
一些批处理技巧
cd %~dp0 //use this if you use 'for xxx in', it solved most of my problems
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
)
echo %errorlevel%
pause
回答by Patricio Cobein
You can use Batch Compileradd-in for SMSS, it let's you run multiple scripts at once, create SQLCMD scripts or consolidate them into a *.sql file.
您可以为 SMSS使用Batch Compiler插件,它让您一次运行多个脚本、创建 SQLCMD 脚本或将它们合并到一个 *.sql 文件中。