您不能将会话中的 sqlplus 变量绑定到函数/过程。它会给你“坏绑定变量”的错误。您实际上可以将绑定变量从您的 oracle 会话传递给任何过程。
让我们看一个例子
variable v1 NUMBER;
begin
select salary into :v1 from employees where employee_id = 100;
dbms_output.put_line(:v1);
end;
/
如果您通过包含在过程/函数中来运行上述示例,它将向您显示错误。
create or replace procedure proc is
begin
select salary into :v1 from employees where employee_id = 100;
dbms_output.put_line(:v1);
end;
/
错误 -
PROCEDURE proc compiled
Warning: execution completed with warning
3/20 PLS-00049: bad bind variable 'V1'
4/22 PLS-00049: bad bind variable 'V1'
因此,不可能在过程/函数中使用会话级绑定变量。在下面的例子中 t2 是一个绑定变量
create or replace procedure proc is
t2 NUMBER;
begin
select salary into t2 from employees where employee_id = 100;
dbms_output.put_line(t2);
end;
/
您可以从 sqlplus 调用此过程
exec proc;