【问题标题】:Get fields with index instead name of column in Oracle PL/SQL在 Oracle PL/SQL 中获取具有索引而不是列名的字段
【发布时间】:2023-03-04 02:11:01
【问题描述】:

有人知道如何通过索引访问 plsql oracle 游标字段。

是否可以通过索引而不是列名来获取表的字段?

提前致谢。

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;

例子

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;

【问题讨论】:

  • 你真正想要达到什么目的?听起来您可能正在寻找uses dbms_sql 的方法,但不知道您为什么不想使用列名,很难说。

标签: sql oracle plsql oracle10g


【解决方案1】:

如果您问是否可以按位置引用 PL/SQL 记录中的字段,答案是否定的。您只能通过名称引用它们。

顺便说一下,在数据库世界中,索引通常是指一个数据库对象,它存储表中的键值以及它们的物理位置,以便快速检索,而 是,哦,没关系,所以 “通过索引获取表的字段” 让我有点困惑,因为我认为您的意思不是存储数据的优化查找。抱歉,如果我有这个错误。

【讨论】:

    【解决方案2】:

    试试这样的:

    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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-25
      • 2011-10-09
      • 2013-07-31
      • 1970-01-01
      • 1970-01-01
      • 2011-04-12
      • 2012-07-05
      相关资源
      最近更新 更多