【发布时间】: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 没有定义,但我必须使用它。你知道吗?
【问题讨论】:
-
您的记录是否有字段
type1、type2和type3?否则,当cntr为 2 或 3 时,我看不出type || cntr会做什么。如果你重复了type、amount和gross3 次,看起来你真的想要@ 987654335@ 是一个包含 3 个元素的集合,然后您可以使用索引来引用这些元素。 -
我的记录包含 type1,amount1,gross1, type2,amount2,gross2 等...
-
我更新了我的记录
-
为什么要记录有
type1、type2和type3,而不是声明为varchar2(50)的集合的type? -
因为数据是这样来的,所以我将数据存储在数组中
标签: oracle for-loop plsql concatenation