问题
This is a trivialized example so it is easy to reproduce, but the important thing is I need to set a variable based on a query using into :someVariable from sometable where sometable.somecolumn = :y
and then leverage both variables after the BEGIN/END block. It seems referencing :y
in the where
causes it to be cleared. Why does this happen?
I am more concerned with why does this happen than how to fix it. I have the work around of setting it with itself, but it seems like an odd side affect. In the first example, I am not declaring a new variable named :y, so I would not think it is an issue with variable hiding. Clearly the second example shows that I can set the value of the variable, and setting it is visible outside the scope of the block, which I would expect since the variable was declared outside the scope of the block.
clear screen;
variable x varchar2(10);
variable y varchar2(10);
exec :y := 'YYY';
BEGIN
select '1' into :x
from dual
where 'YYY' = :y;
END;
/
select :y from dual;
Output shows :y is cleared:
PL/SQL procedure successfully completed.
PL/SQL procedure successfully completed.
:Y
--------------------------------
If I set :y with itself, the value is preserved:
clear screen;
variable x varchar2(10);
variable y varchar2(10);
exec :y := 'YYY';
BEGIN
select '1' into :x
from dual
where 'YYY' = :y;
:y := :y;
END;
/
select :y from dual;
Output:
PL/SQL procedure successfully completed.
PL/SQL procedure successfully completed.
:Y
--------------------------------
YYY
回答1:
Upgraded to SQL Developer 17.2.0.188 and it went away.
Appears to be this bug: https://stackoverflow.com/a/43501389/84206
来源:https://stackoverflow.com/questions/43401127/bind-variable-used-in-begin-end-gets-cleared