传递给 SQL*Plus 脚本的参数会覆盖调用它的脚本的参数

Arguments passed to SQL*Plus script override the arguments of script which invoked it

file2.sql

set serveroutput on
exec dbms_output.put_line('invoke file2.sql');

file1.sql

exec dbms_output.put_line('&1');
@file2.sql 'col2' 'col1'
exec dbms_output.put_line('&1');

我正在执行@file1.sql'col1'。请在下面找到输出。

@f1.sqlcol1

col1

PL/SQL 程序成功完成。

调用file2.sql

PL/SQL 程序成功完成。

col2

PL/SQL 程序成功完成。

SQL>

我希望在调用 file2.sql 后打印 'col2',但它被传递给 file2.sql 的值覆盖了。如何克服这个问题?

您可以将原始参数保存在新变量中:

file1.sql:

prompt Argument 1 = &1
define somevar = &1

@file2.sql col2 col1

prompt Original argument 1 = &somevar

输出:

SQL> @file1.sql col1
Argument 1 = col1
This is file2.sql
Original argument 1 = col1