【问题标题】:Loop variable with concatenation in pl/sql在 pl/sql 中具有连接的循环变量
【发布时间】:2021-06-02 19:36:26
【问题描述】:

我在 pl/sql 中有一个循环。我想使用带有字符串的循环变量,实际上是数组值

     FETCH REFCUR BULK COLLECT INTO MY_ARRAY;
     FOR indx IN 1 .. MY_ARRAY.COUNT LOOP
         FOR cntr IN 1..3
          LOOP
           student_rec := student_type_wr(null, null, null, null);
           student_rec.invoice_date := MY_ARRAY(indx).invoice_date;
           student_rec.service_type := MY_ARRAY(indx).type || cntr; // it gives compile error
           student_rec.amount := MY_ARRAY(indx).amount || cntr;   // it gives compile error
           student_rec.gross := MY_ARRAY(indx).gross || cntr;     // it gives compile error        
           student_recs .extend();
           student_recs(student_recs.count()) := student_rec;
         END LOOP;

        END LOOP;

我的数组是这样的:

   TYPE a_type IS RECORD (invoice_date DATE,
                     type1 VARCHAR2(50),
                     amount1 NUMBER,
                     gross1 NUMBER,
                     type2 VARCHAR2(50),
                     amount2 NUMBER,
                     gross2 NUMBER,
                     type3 VARCHAR2(50),
                     amount3 NUMBER,
                     gross3 NUMBER,);
   TYPE TABLETYPE IS TABLE OF a_type;
   MY_ARRAY TABLETYPE;

如何将循环变量与数组字段连接起来?我想用MY_ARRAY(indx).type || cntr阅读MY_ARRAY(indx).type1

我的错误是:

Error(70,66): PLS-00302: component 'TYPE' must be declared
Error(71,60): PLS-00302: component 'AMOUNT' must be declared
Error(72,66): PLS-00302: component 'GROSS' must be declared

我知道我的错误MY_ARRAY(indx).type 没有定义,但我必须使用它。你知道吗?

【问题讨论】:

  • 您的记录是否有字段type1type2type3?否则,当cntr 为 2 或 3 时,我看不出type || cntr 会做什么。如果你重复了typeamountgross 3 次,看起来你真的想要@ 987654335@ 是一个包含 3 个元素的集合,然后您可以使用索引来引用这些元素。
  • 我的记录包含 type1,amount1,gross1, type2,amount2,gross2 等...
  • 我更新了我的记录
  • 为什么要记录有type1type2type3,而不是声明为varchar2(50)的集合的type
  • 因为数据是这样来的,所以我将数据存储在数组中

标签: oracle for-loop plsql concatenation


【解决方案1】:

据我了解,您的代码是您希望动态调用列,而据我所知,Oracle 是您不能这样做。所以你必须使用下面的代码 -

FETCH REFCUR BULK COLLECT INTO MY_ARRAY;
     FOR indx IN 1 .. MY_ARRAY.COUNT LOOP
           student_rec := student_type_wr(null, null, null, null);
           student_rec.invoice_date := MY_ARRAY(indx).invoice_date;
           student_rec.service_type := MY_ARRAY(indx).type1;
           student_rec.amount := MY_ARRAY(indx).amount1;
           student_rec.gross := MY_ARRAY(indx).gross1;
           student_recs .extend();
           student_recs(student_recs.count()) := student_rec;
           student_rec := student_type_wr(null, null, null, null);
           student_rec.invoice_date := MY_ARRAY(indx).invoice_date;
           student_rec.service_type := MY_ARRAY(indx).type2;
           student_rec.amount := MY_ARRAY(indx).amount2;
           student_rec.gross := MY_ARRAY(indx).gross2;
           student_recs .extend();
           student_recs(student_recs.count()) := student_rec;
           student_rec := student_type_wr(null, null, null, null);
           student_rec.invoice_date := MY_ARRAY(indx).invoice_date;
           student_rec.service_type := MY_ARRAY(indx).type3;
           student_rec.amount := MY_ARRAY(indx).amount3;
           student_rec.gross := MY_ARRAY(indx).gross3;
           student_recs .extend();
           student_recs(student_recs.count()) := student_rec;
         END LOOP;

这里值得一试的是使用EXECUTE IMMEDIATE 声明如下 -

FETCH REFCUR BULK COLLECT INTO MY_ARRAY;
     FOR indx IN 1 .. MY_ARRAY.COUNT LOOP
         FOR cntr IN 1..3
          LOOP
           student_rec := student_type_wr(null, null, null, null);
           student_rec.invoice_date := MY_ARRAY(indx).invoice_date;
           EXECUTE IMMEDIATE 'student_rec.service_type := MY_ARRAY(indx).type' || cntr;
           EXECUTE IMMEDIATE 'student_rec.amount := MY_ARRAY(indx).amount' || cntr;
           EXECUTE IMMEDIATE 'student_rec.gross := MY_ARRAY(indx).gross' || cntr;
           student_recs .extend();
           student_recs(student_recs.count()) := student_rec;
         END LOOP;

        END LOOP;

虽然我以前从未尝试过这个,但我知道这行不通。

【讨论】:

  • 昨天我把你的第一个解决方案写成了最后一个解决方案 :) 谢谢你的帮助
猜你喜欢
  • 1970-01-01
  • 2016-04-22
  • 1970-01-01
  • 2012-10-05
  • 2017-02-24
  • 1970-01-01
  • 1970-01-01
  • 2013-09-13
  • 1970-01-01
相关资源
最近更新 更多