我在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
没有定义,但我必须使用它。你知道吗?
据我所知,你的代码是你想动态调用你的列,而据我所知,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;
虽然我以前从未尝试过,但我的知识告诉我这是行不通的。