如何从批处理文件运行 .sql 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3294545/
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 .sql file from a batch file?
提问by Vishal
I am running sql server 2008 express and i need to schedule some stored procedures to run nightly...so i have built out these .sql files which i would want to run from .bat file...i need to know the command to execute these .sql files one by one and store their results i guess...can anyone help me out?
我正在运行 sql server 2008 express,我需要安排一些存储过程每晚运行...所以我已经构建了这些我想从 .bat 文件运行的 .sql 文件...我需要知道命令一个一个地执行这些 .sql 文件并存储它们的结果,我猜......有人可以帮我吗?
回答by driis
I answered this in this other question:
You should invoke the sqlcmdcommand-line tool from your batch file. Assuming your sql file is "backup.sql", the command line would be something like:
您应该从批处理文件中调用sqlcmd命令行工具。假设您的 sql 文件是“backup.sql”,命令行将类似于:
sqlcmd -E -S yoursqlinstance -i backup.sql
-E
uses trusted connection, replace with -U
and -P
if you need to specify a SQL username and password. See also this article with examples.
-E
使用受信任的连接,以更换-U
和-P
如果需要指定SQL用户名和密码。另请参阅本文的示例。
回答by marc_s
See the sqlcmd
utility:
查看sqlcmd
实用程序:
http://msdn.microsoft.com/en-us/library/ms165702.aspx
http://msdn.microsoft.com/en-us/library/ms165702.aspx
This allows you to run sql scripts from the command line
这允许您从命令行运行 sql 脚本
回答by Tahbaza
回答by Lèse majesté
I don't use SQL Server, but a batch file is just a list of DOS commands. So whatever you use to execute SQL files from the commandline can be used in a batch file.
我不使用 SQL Server,但批处理文件只是 DOS 命令的列表。因此,您用于从命令行执行 SQL 文件的任何内容都可以在批处理文件中使用。
A quick google search turns up:
一个快速的谷歌搜索出现:
sqlcmd -i <inputfile> -o <outputfile>
回答by Shessuky
Hope this helps you :
希望这可以帮助您:
sqlplus UserName/Password@DataBase @C:\sqlFolder\sqlFile.sql
P.S : Don't forget to add the command "commit;" at the end of sql file (sqlFile.sql), this command order Oracle to save performed changes in database
PS:不要忘记添加命令“commit;” 在 sql 文件 (sqlFile.sql) 的末尾,此命令命令 Oracle 将执行的更改保存在数据库中