Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable field name

Status
Not open for further replies.

jdmartin74

Programmer
Sep 13, 2002
45
0
0
US
I have a function which is passed a value. I wish to make up a field name based on that value:

v_field := 'ADDRESS_' || p_line;

The above line would give v_field a value of 'ADDRESS_1' for example.

I then want to do:

SELECT v_field INTO v_address from COMPANIES;

This works in so far as the value of v_address becomes 'ADDRESS_1' in this case, and not the value of that field if I had:

SELECT ADDRESS_1 INTO v_address from COMPANIES;

Does anyone know how I can in affect use a variable field name?

Thanks
 
JD,

Consider this code:
Code:
select * from companies;

NAME            ADDRESS_1       ADDRESS_2
--------------- --------------- ---------------
Ajax            123 Anystreet   Anytown, USA

declare
    sql_stm   varchar2(100);
    v_field   varchar2(30);
    v_address varchar2(100);
begin
    for p_line in 1..2 loop
        v_field := 'ADDRESS_' || p_line;
        sql_stm := 'SELECT '||v_field||' from COMPANIES';
        execute immediate sql_stm into v_address;
        dbms_output.put_line(v_address);
    end loop;
end;
/

123 Anystreet
Anytown, USA

PL/SQL procedure successfully completed.
Let us know if this model provides you the capabilities for which you are looking.


[santa]Mufasa
(aka Dave of Sandy, Utah, USA)
[ Providing low-cost remote Database Admin services]
Click here to join Utah Oracle Users Group on Tek-Tips if you use Oracle in Utah USA.
 
I'm not in a loop, but the lines:

sql_stm := 'SELECT '||v_field||' from COMPANIES';
execute immediate sql_stm into v_address;

are exactly what I needed.

Thanks so much.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top