Skip to Main Content

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Vaijanath.

Asked: July 23, 2007 - 3:30 am UTC

Last updated: November 27, 2015 - 2:38 am UTC

Version: 10.2.0.3.0

Viewed 10K+ times! This question is

You Asked

Hi Tom,
Trust you are doing well.I have bought your two books "Expert Oracle" and "Effective Oracle by Design" and I must admit, I have become your BIG fan.

I have a question on variable scope and its accessibility in PL/SQL.

Suppose I have a block as below.

Declare -- Block 1
x number :=1;
Begin
Declare -- Block 2
x number :=2;
Begin
Declare -- Block 3
x number:=3;
Begin
-- n inner blocks;
End;
End;
End;

Now, the question is if I have same variable in nested blocks, and I want to access the variable (of same name that of inner block) of outer block ( Variable of block 2 in Block 3), how do I access it.More, If I want to access variable of same name that exists in nth outer block variable ?
Say from Block 3 variable of Block 1 ?

Thanks in Advance,

Vaijanath
India

and Tom said...

I would say:

DO NOT DO THIS, how ugly is that code.... You should keep code readable and maintainable - not purposely do things like this that make it utterly impossible to follow.


If you label your blocks, you can reference them:

ops$tkyte%ORA10GR2> <<topmost>>
  2  declare
  3     x varchar2(30) := 'top most';
  4  begin
  5     <<middle>>
  6     declare
  7        x varchar2(30) := 'middle';
  8     begin
  9        <<bottom>>
 10        declare
 11           x varchar2(30) := 'bottom';
 12        begin
 13           dbms_output.put_line( 'currently in scope is ' || x );
 14           dbms_output.put_line( topmost.x );
 15           dbms_output.put_line( middle.x );
 16           dbms_output.put_line( bottom.x );
 17        end;
 18     end;
 19  end;
 20  /
currently in scope is bottom
top most
middle
bottom

PL/SQL procedure successfully completed.

Rating

  (5 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Absolute knowledge corrupts absolutely

Chuck Jolley, July 23, 2007 - 2:25 pm UTC

Tom,
1, Is there some way to have that fact removed from my brain?
2, Is there some way to block access to that answer from our domain for our other programmers?
3, Does this mean PL/SQL supports GOTO?

Seriously, are there any useful uses for labels in PL/SQL code?


Tom Kyte
July 24, 2007 - 9:04 am UTC

3) yes, indeed it does.

Absolute Knowledge

Vaijanath, July 24, 2007 - 1:21 am UTC

I think our friend Chuck Jolley is over reacting a bit. A practical use of above is indeed rare (and not desirable) but there is no problem in throwing light on the matter.
Tom Kyte
July 24, 2007 - 9:40 am UTC

I think there was humor involved in Chuck's response :)

Tongue in cheek.


If something is rare (and not desirable), then I would say it should be non-existent myself....

A reader, July 24, 2007 - 11:41 am UTC

Well of course I was being humorous.
I really only wanted to block access by our less experienced programmers. ;)


didn't know about the labels

James, July 25, 2007 - 3:07 am UTC

Glad I found out about this!

It's usefull for testing a section of code in an anonymous pl/sql block that was ripped from a procedure that might reference some variables with a scope label.

In the past I would have to manually remove all references
to my_proc.variable_name and just use variable_name if I wanted to test it.

Now i just start my block with

<<my_proc>>

sweet!


Mayur Pawar, November 26, 2015 - 9:36 pm UTC

Thank You it was really useful for me..!!
Chris Saxon
November 27, 2015 - 2:38 am UTC

Thank you for the feedback

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library