oracle 如何使用 SQLPLUS 在命令行上将参数传递给 PL/SQL 脚本?

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

How do I pass arguments to a PL/SQL script on command line with SQLPLUS?

oraclesqlplus

提问by djangofan

How do I pass arguments to a PL/SQL script on command line with SQLPLUS? I can call my PL/SQL script like so, but the script requires arguments in order for it to succeed. How can I run sqlplus.exe so that I can pass arguments to the script?

如何使用 SQLPLUS 在命令行上将参数传递给 PL/SQL 脚本?我可以像这样调用我的 PL/SQL 脚本,但该脚本需要参数才能成功。如何运行 sqlplus.exe 以便我可以将参数传递给脚本?

@ECHO off
// where HOST030 is a tnsnames alias to a machine, port, and instance    
sqlplus.exe MYUSER/mypassword@HOST030 < refreshDataOnOracle.sql    
pause

I tried to search for the answer but couldn't find an "argument example" anywhere for SQLPLUS. I suspect it would be a similar method of using the SQL*Plus "START" command?

我试图搜索答案,但在任何地方都找不到 SQLPLUS 的“参数示例”。我怀疑这将是使用 SQL*Plus“START”命令的类似方法?

回答by mustaccio

Firstly, you will need to invoke your script like so:

首先,您需要像这样调用脚本:

sqlplus.exe MYUSER/mypassword@HOST030 @refreshDataOnOracle.sql foo bar  

Instead of the OS redirection you will use the "@" symbol to indicate the file name to execute. You will also supply the script parameters on the command line. In the script you will refer to the parameters using &1, &2etc.

您将使用“@”符号来指示要执行的文件名,而不是操作系统重定向。您还将在命令行上提供脚本参数。在脚本中,您将引用参数使用&1&2等等。

update mytable set mycol = '&2' where myid = '&1';

which will translate into

这将转化为

update mytable set mycol = 'bar' where myid = 'foo';

回答by PolyMorph

If you want log the run of sqlplus, you can use this syntax:

如果要记录 sqlplus 的运行,可以使用以下语法:

sqlplus.exe MYUSER/mypassword@HOST030 @refreshDataOnOracle.sql foo bar  >> log.log