如何将参数传递给 sqlplus 中使用 START 调用的 sql 文件中的 PL/SQL 块?

2021-12-24 00:00:00 oracle plsql sqlplus

我有一个通过 sqlplus 运行多个 sql 文件的 bash 脚本:

I have a bash script running several sql files via sqlplus:

sqlplus $connectioninfo << end
start file1.sql
start file2.sql
start file3.sql $variable
quit
end

file3 有一些 PL/SQL:

file3 has some PL/SQL:

BEGIN

  DBMS_OUTPUT.PUT_LINE(&1);

END;
/

但它只是打印文字 "&1" 而不是 $variable 的值.我也在 file3 中尝试了以下内容:

But it just prints the literal "&1" instead of the value of $variable. I have also tried the following in file3:

DEFINE var_a = &1;
BEGIN

  DBMS_OUTPUT.PUT_LINE(var_a);

END;
/

还有以下内容:

DECLARE
  var_b VARCHAR2(64) := &1;

BEGIN

  DBMS_OUTPUT.PUT_LINE(var_b);

END;
/

最后:

DEFINE var_a = &1;

DECLARE
  var_b VARCHAR2(64) := var_a;

BEGIN

  DBMS_OUTPUT.PUT_LINE(var_b);

END;
/

但是,我收到了各种错误,或者只是所有这些错误的字面值&1".

However, I am getting various errors or just the literal value '&1' for all of these.

推荐答案

尝试将 SET DEFINE ON 添加到脚本的开头 file3.sql.

Try adding SET DEFINE ON to the start of your script file3.sql.

如果SET DEFINEON,SQL*Plus 将用它们的值替换&... 替换参数.如果 SET DEFINEOFF(它似乎适合您),SQL*Plus 不会这样做.

If SET DEFINE is ON, SQL*Plus will replace &... substitution parameters with their values. If SET DEFINE is OFF (which it seems to be for you), SQL*Plus won't do this.

相关文章