How to get field name dynamically in oracle pl/sql query? -


i have table contains fields named 'value1','value2', 'value3'. in sql code have variable selectedvalue of type number gets value dynamically inside loop in code. have simple query this:

select value1 val_table; 

so want select value1 when selectedvalue = 1, value2 when selectedvalue = 2, value3 when selectedvalue = 3.

how can field name dynamically in select statement depending on value of selectedvalue variable?

the simplest thing be:

declare   anumber  number; begin   case selectedvalue     when 1 select value1 anumber val_table;     when 2 select value2 anumber val_table;     when 3 select value3 anumber val_table;   end case;    dbms_output.put_line('anumber=' || anumber); end; 

if want make more complicated can following:

declare   acursor  sys_refcursor;   strquery varchar2(1000);   strfield varchar2(20);   anumber  number; begin   case selectedvalue     when 1 strfield := 'value1';     when 2 strfield := 'value2';     when 3 strfield := 'value3';   end case;    strquery := 'select ' || strfield || ' val_table';    open acursor strquery;    loop     fetch acursor anumber;     exit when acursor%notfound;      dbms_output.put_line('anumber=' || anumber);   end loop;    close acursor; end; 

share , enjoy.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -