Extracting a variable value [message #214455] |
Tue, 16 January 2007 08:56 |
scorpio_biker
Messages: 154 Registered: November 2005 Location: Kent, England
|
Senior Member |
|
|
Hi,
I wonder if anyone could point me in the right direction with this query? I'm not sure if I can actually do this?
I have a variable that exists in a package e.g.
In a table I have a field that contains the name of the variable. Is it possible for me to get the value of the variable whose name is stored in the table.
I have tried using dynamic sql but that just pulls the string name back
declare
cur PLS_INTEGER := DBMS_SQL.OPEN_CURSOR;
fdbk PLS_INTEGER;
local_var varchar2(100);
var_1 varchar2(100) := 'retentioncell';
retentioncell number;
begin
retentioncell := 1;
DBMS_SQL.PARSE (cur, 'BEGIN :tmp := :tmp2; END;' ,2);
DBMS_SQL.BIND_VARIABLE (cur, 'tmp', ' ');
DBMS_SQL.BIND_VARIABLE (cur, 'tmp2', var_1);
fdbk := DBMS_SQL.EXECUTE (cur);
DBMS_SQL.VARIABLE_VALUE (cur, 'tmp', local_var);
dbms_output.put_line(local_var);
end;
My dbms_output shows retentioncell - reasonably enough, but I want to get the value 1.
So I really need to do a kind of double dynamic sql where I get the name and then the value. Can I do this? We have recently upgraded from 7.3 to 9i so there may well be new functionality that I'm not aware of?
I appreciate it's a strange request but I wanted to table drive some logic checking rather than having to hard code package values every time Marketing change their mind.
|
|
|
Re: Extracting a variable value [message #214461 is a reply to message #214455] |
Tue, 16 January 2007 09:06 |
dmitry.nikiforov
Messages: 723 Registered: March 2005
|
Senior Member |
|
|
Technically there is not any problem:
SQL> create or replace package pkg_t
2 is
3 public_var number := 10;
4 end;
5 /
Package created.
SQL> declare
2 varname varchar2(30) := 'public_var';
3 value number;
4 begin
5 execute immediate 'begin :1 := pkg_t.' || varname || '; end;' using out value;
6 dbms_output.put_line(value);
7 end;
8 /
10
PL/SQL procedure successfully completed.
But the purpose of this design is quite unclear.
Rgds.
|
|
|
|