How to use value of variable as variable?

Viewed 53

I want to use value of the variable as a new variable. But i don't know how to use it.

DECLARE
    example1 varchar2(10) = 'AAA';
    AAA varchar2(10) = 'BBB';
BEGIN
    dbms_output.put_line(?example1); -- Output must be 'BBB'.
END;

Which character must be come instead of "?" to achieve 'BBB' output? Do you have any idea about that?

Thanks.

2 Answers

Perhaps you can achieve what you want with an associative array.

declare
    type string_aa is table of varchar2(100) index by varchar2(100);
    my_list string_aa;

    example1 varchar2(10) := 'AAA';

begin
    my_list('AAA') := 'BBB';

    dbms_output.put_line(my_list(example1)); 
end;

One method to solve this problem may be to use dynamic SQL to access the variables.

DECLARE
example1 varchar2(10) := 'AAA';
AAA varchar2(10) := 'BBB';
x varchar2(1000);
BEGIN
  x := 'declare '||example1||' varchar2(10) := '''||AAA||'''; begin dbms_output.put_line('||example1||'); end;';
  execute immediate x;
END;
Related