.bat 文件可以执行 sql 查询并返回值吗?
如何从 .bat 文件调用查询? (假设我的查询是:从系统中选择版本)。
我的 .bat 文件可以保存此查询返回的输出吗?我想在我的 NSIS 脚本中使用这个输出。
How can I call a query from a .bat file? (say my query is: select version from system).
Can my .bat file save the output that this query returns? I wanna use this output in my NSIS script.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
对于 Oracle:
如何通过 sql plus 从命令行发出单个命令?
您可以将输出通过管道传输到文件并使用它,或者将其包装在
for
命令中解析批处理文件中的输出。For Oracle:
How can I issue a single command from the command line through sql plus?
You can either pipe the output to a file and use that, or wrap this in a
for
command to parse the output within your batch file.编辑:这个答案适用于 SQL Server,而不是 Oracle。(问题发布时并不清楚)。
您可能需要检查 sqlcmd 实用程序。
sqlcmd 实用程序允许您在命令提示符处输入 Transact-SQL 语句、系统过程和脚本文件。以下示例在 sqlcmd 启动时执行查询,然后立即退出。可以执行多个分号分隔的查询:
要将查询的输出保存到文件中,可以使用
-o C:\<文件名>
选项。EDIT: This answer is for SQL Server, not Oracle. (It wasn't immediately clear when the question was posted).
You may want to check the sqlcmd utility.
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:
To save the output of the query to a file, you can use the
-o C:\< filename>
option.这取决于您使用什么工具来执行查询。例如,对于 mysql 命令行工具,您' d do:
这里,
mysql -u的部分-p<密码>; -h
是连接到服务器的标准选项。-Nrs
是一组选项,将导致客户端不输出结果的所有 ascii art。-e"SELECT version() FROM system"
中的位实际上指定了应该执行的命令。请参阅 http://dev.mysql.com/doc /refman/5.1/en/mysql-command-options.html 了解有关这些 mysql 特定选项的更多信息。在您要使用的客户端工具中查找类似的选项。最后一位,
> out.txt
是标准操作系统级重定向。它将导致来自 mysql 程序的输出存储在文件out.txt
中,但也许您也可以直接重定向到您的 NSIS 程序。This depends on what tool you use to execute queries. For example, for the mysql command line tool, you'd do:
Here, the piece that goes
mysql -u<user> -p<password> -h<host>
are the standard options to connect to the server.-Nrs
are 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.html for more info on those mysql-specific options. Look for similar options in the client tool you want to use.The last bit,
> out.txt
is a standard operating system-level redirect. It will cause the output coming from the mysql program to be stored in a fileout.txt
, but perhaps you can redirect directly to your NSIS program as well.尝试
其中 query.sql 包含数据库查询,result.txt 将填充查询输出。
根据查询的不同,输出文件可能不符合您的预期(典型的 sqlplus 的列标题等),因此您的 query.sql 应该包含一些 SET 命令来根据您的需要调整输出。
另一种方法是使用 Oracle SPOOL 命令,而不是将结果通过管道传输到文件。
Try
where query.sql contains the database query and result.txt will be filled with the query output.
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.
Another method is to use the Oracle SPOOL command instead of piping the result to a file.
试试这个:
http://www.dbforums.com/oracle /1044496-sqlplus-return-value-help.html
Try this:
http://www.dbforums.com/oracle/1044496-sqlplus-return-value-help.html
如果您有命令行 SQL 实用程序,那么它很可能允许命令行操作。根据你的SQL版本你可以检查它。
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.