从 pl/sql 中的只写(OUT)参数读取
当我尝试写入函数的只读参数(IN)时,Oracle 抱怨错误。但从函数的只写 (OUT) 参数读取时,情况并非如此。 Oracle 默默地允许这样做,不会出现任何错误。这种行为的原因是什么? 以下代码执行时不会对“so”变量进行任何赋值:
create or replace function foo(a OUT number) return number
is
so number;
begin
so := a; --no assignment happens here
a := 42;
dbms_output.put_line('HiYA there');
dbms_output.put_line('VAlue:' || so);
return 5;
end;
/
declare
somevar number;
a number := 6;
begin
dbms_output.put_line('Before a:'|| a);
somevar := foo(a);
dbms_output.put_line('After a:' || a);
end;
/
这是我得到的输出:
Before a:6
HiYA there
VAlue:
After a:42
When I tried writing to an read-only parameter(IN) of a function, Oracle complains with an error. But that is not the case when reading from an write-only(OUT) parameter of a function. Oracle silently allows this without any error. What is the reason for this behaviour?.
The following code executes without any assignment happening to "so" variable:
create or replace function foo(a OUT number) return number
is
so number;
begin
so := a; --no assignment happens here
a := 42;
dbms_output.put_line('HiYA there');
dbms_output.put_line('VAlue:' || so);
return 5;
end;
/
declare
somevar number;
a number := 6;
begin
dbms_output.put_line('Before a:'|| a);
somevar := foo(a);
dbms_output.put_line('After a:' || a);
end;
/
Here's the output I got:
Before a:6
HiYA there
VAlue:
After a:42
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
允许从 OUT 参数读取:您可以在过程开始时在 OUT 参数中写入内容,并且您可能希望在返回之前读取它包含的值,这不是一个错误。
这里发生的情况是,由于它是 OUT 参数而不是 IN OUT 参数,因此
a
的值不会传递给函数foo
,因此在开头过程中 OUT 参数a
包含NULL
值。您可以通过注释掉a := 42;
行来检查这一点:Reading from an OUT parameter is allowed: you could have written things in your OUT parameter at the beginning of a procedure and you might want to read the value it contains before returning, this is not a bug.
What happens here is that since it is an OUT parameter and not an IN OUT parameter, the value of
a
is not passed to the functionfoo
, so at the beginning of the procedure the OUT parametera
contains theNULL
value. You can check this by commenting out the linea := 42;
: