【问题标题】:Error trying to print Collection variable by index尝试按索引打印集合变量时出错
【发布时间】:2014-09-23 12:15:13
【问题描述】:

任何人都可以确定以下代码的问题所在。特别是第一个 dbms_output 行。第二个打印得很好。但是第一个给出了这个错误:

第 2 行出错 ORA-06550:第 15 行,第 53 列: PLS-00201:必须声明标识符“MYCOLL” ORA-06550:第 15 行,第 1 列: PL/SQL:语句被忽略

DECLARE
   CURSOR c1
   IS
      SELECT sub_provider_address_id sub_id, phone, extension
        FROM sub_provider_address;

   TYPE coll_type IS TABLE OF c1%ROWTYPE;
   my_coll   coll_type;
BEGIN
   OPEN c1;

   FETCH c1
   BULK COLLECT INTO my_coll;

dbms_output.put_line(' my_coll first row id has '|| mycoll(1).phone );
dbms_output.put_line(' my_coll now has '|| my_coll.last );

END;

【问题讨论】:

  • 别忘了关闭光标
  • 这是 my_coll 不是 mycoll!问题解决了!

标签: oracle collections plsql


【解决方案1】:

您将变量声明为my_coll

my_coll   coll_type;

在出错的那一行,您将其称为mycoll

dbms_output.put_line(' my_coll first row id has '|| mycoll(1).phone );

所以你只是缺少一个下划线,它应该是:

dbms_output.put_line(' my_coll first row id has '|| my_coll(1).phone );

【讨论】:

    【解决方案2】:

    您的coll_type 不是关联的数组类型。 你必须使用这样的东西:

    TYPE coll_type IS TABLE OF c1%ROWTYPE index by pls_integer;
    

    以此为例:

    declare
      cursor c1 is
        select 1 id from dual
      ;
      type coll_type is table of c1%ROWTYPE index by pls_integer;
      l_coll_type coll_type;
    begin
      open c1;
      fetch c1 bulk collect into l_coll_type;
      close c1;
      dbms_output.put_line('Output:' || l_coll_type(1).id);
    end;
    
    Output:1
    

    【讨论】:

    • 我按照您的建议在声明中添加了“pls_integer 索引”。那仍然没有帮助。打印 mycoll(i).phone 的行仍然导致相同的错误。
    • 你的变量imycoll(i).phone 中声明了吗?
    猜你喜欢
    • 2021-04-17
    • 2014-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-11
    • 1970-01-01
    • 2011-06-25
    相关资源
    最近更新 更多