如何从批处理文件运行 .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

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

How to run .sql file from a batch file?

sqlwindowssql-server-2008scheduling

提问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

-Euses trusted connection, replace with -Uand -Pif you need to specify a SQL username and password. See also this article with examples.

-E使用受信任的连接,以更换-U-P如果需要指定SQL用户名和密码。另请参阅本文的示例

回答by marc_s

See the sqlcmdutility:

查看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 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 将执行的更改保存在数据库中