plsql - Oracle, how to declare local variable inside executable block -
typically variables declared in declare section, , available inside begin block. find style stiff , tedious.
is possible declare variables inside begin block when needed? stupid declare new global variable, if it's needed store temporary value further calculations, queries , assertions.
try - added comments in code understand visibility domain of each variable. can remove comment last dbms_output see var2 no longer available in outer code.
set serveroutput on; <<tag>> declare var1 int := 1; -- global variable begin declare var1 int := 2; -- local variable var2 int := 0; begin dbms_output.put_line(var1); -- display 2 (value of local var1); dbms_output.put_line(tag.var1); -- display 1 (value of global var1); dbms_output.put_line(var2); -- display 0 (value of local var2); end; dbms_output.put_line(var1); -- display 1 (value of global var1); -- dbms_output.put_line(var2); -- crash since var2 no longer in memory; end;
Comments
Post a Comment