Juan Carlos Quishpe
Juan Carlos Quishpe

Reputation: 69

Get fields with index instead name of column in Oracle PL/SQL

Someone knows how to access the plsql oracle cursor fields through an index.

Is it possible to get the fields of the table through an index instead of the name of column?

Thanks in advance.

declare
  cursor c_user is 
    select * 
      from users
     where age > 20;
begin
    for u in c_user loop
        dbms_output.put_line(u.lastname||' '||u.firstname);
    end loop; 
end;

Example

declare
  cursor c_user is 
    select * 
      from users
     where age > 20;
begin
    for u in c_user loop
        dbms_output.put_line(u.1||' '||u.2);
    end loop; 
end;

Upvotes: 0

Views: 1559

Answers (2)

William Robertson
William Robertson

Reputation: 15991

If you are asking whether you can refer to the fields in a PL/SQL record by their position, the answer is no. You can only refer to them by name.

By the way, in the database world, an index usually refers to a database object that stores key values from a table together with their physical location organised for fast retrieval, and a table is, oh never mind, so "get the fields of the table through an index" had me a bit confused because I don't think you mean an optimised lookup of stored data. Apologies if I have this wrong.

Upvotes: 2

Fadil Zitawi
Fadil Zitawi

Reputation: 164

Try some thing like this:

declare
cursor Inx_Cur(P_Indx Number) is select 
decode(P_Indx,1,ACCOUNTNO,2,ACCOUNTNAME,3,ACCOUNTTYPE) My_field
from accounts;
begin
for Rec in Inx_Cur(3) -- pass the required index as 
parameter
loop
 dbms_output.put_line('My_field : '||Rec.My_field);
end loop;
end;

Upvotes: 0

Related Questions