.bat 文件可以执行 sql 查询并返回值吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2047882/
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 05:03:43  来源:igfitidea点击:

Can .bat file execute an sql query and return a value?

sqloraclebatch-filereturn-valuensis

提问by Pia

How can I call a query from a .bat file? (say my query is: select version from system).

如何从 .bat 文件调用查询?(假设我的查询是:从系统中选择版本)。

Can my .bat file save the output that this query returns? I wanna use this output in my NSIS script.

我的 .bat 文件可以保存此查询返回的输出吗?我想在我的 NSIS 脚本中使用这个输出。

回答by Patrick Cuff

For Oracle:

对于甲骨文:

How can I issue a single command from the command line through sql plus?

如何通过 sql plus 从命令行发出单个命令?

@echo select version from system; | sqlplus username/password@database 

You can either pipe the output to a file and use that, or wrap this in a forcommand to parse the output within your batch file.

您可以将输出通过管道传输到文件并使用它,或者将其包装在for命令中以解析批处理文件中的输出。

回答by Daniel Vassallo

EDIT: This answer is for SQL Server, not Oracle.(It wasn't immediately clear when the question was posted).

编辑:此答案适用于 SQL Server,而不是 Oracle。(当问题发布时并不清楚)。

You may want to check the sqlcmdutility.

您可能需要检查sqlcmd实用程序。

The sqlcmd utility lets you enter Transact-SQL statements, system procedures, and script files at the command prompt. The following example executes a query when sqlcmd starts and then exits immediately. Multiple-semicolon-delimited queries can be executed:

sqlcmd 实用程序允许您在命令提示符下输入 Transact-SQL 语句、系统过程和脚本文件。以下示例在 sqlcmd 启动时执行查询,然后立即退出。可以执行多个分号分隔的查询:

sqlcmd -d AdventureWorks -Q "SELECT FirstName, LastName FROM Person.Contact WHERE LastName LIKE 'Whi%';"

To save the output of the query to a file, you can use the -o C:\< filename>option.

要将查询的输出保存到文件,您可以使用该-o C:\< filename>选项。

回答by Roland Bouman

This depends on what tool you use to execute queries. For example, for the mysql command line tool, you'd do:

这取决于您用于执行查询的工具。例如,对于mysql 命令行工具,您可以执行以下操作:

mysql -u<user> -p<password> -h<host> -Nrs -e"SELECT version() FROM system" > out.txt

Here, the piece that goes mysql -u<user> -p<password> -h<host>are the standard options to connect to the server. -Nrsare a set of options that will cause the client to not output all the ascii art for the results. The bit that goes -e"SELECT version() FROM system"actually specifies the command that should be executed. See http://dev.mysql.com/doc/refman/5.1/en/mysql-command-options.htmlfor more info on those mysql-specific options. Look for similar options in the client tool you want to use.

在这里,去的部分mysql -u<user> -p<password> -h<host>是连接到服务器的标准选项。-Nrs是一组选项,将导致客户端不输出结果的所有 ascii 艺术。这个位-e"SELECT version() FROM system"实际上指定了应该执行的命令。有关这些特定于 mysql 的选项的更多信息,请参阅http://dev.mysql.com/doc/refman/5.1/en/mysql-command-options.html。在您要使用的客户端工具中寻找类似的选项。

The last bit, > out.txtis a standard operating system-level redirect. It will cause the output coming from the mysql program to be stored in a file out.txt, but perhaps you can redirect directly to your NSIS program as well.

最后一点> out.txt是标准的操作系统级重定向。它会导致来自 mysql 程序的输出存储在一个文件中out.txt,但也许您也可以直接重定向到您的 NSIS 程序。

回答by devio

Try

尝试

sqlplus user/pwd@mydb @query.sql > result.txt

where query.sql contains the database query and result.txt will be filled with the query output.

其中 query.sql 包含数据库查询,result.txt 将填充查询输出。

Depending on the query, the output file may not be as you expect it (column headers etc as typical of sqlplus), so your query.sql should contain some SET commands to adjust the output to your needs.

根据查询,输出文件可能与您期望的不同(列标题等是 sqlplus 的典型特征),因此您的 query.sql 应包含一些 SET 命令以根据您的需要调整输出。

Another method is to use the Oracle SPOOL command instead of piping the result to a file.

另一种方法是使用 Oracle SPOOL 命令而不是将结果通过管道传输到文件。

回答by alemjerus

If you have a command-line SQL utility, that most probably it allows command-line operations. According to your SQL-version you can check it.

如果您有命令行 SQL 实用程序,那么它很可能允许命令行操作。根据您的 SQL 版本,您可以检查它。