2015-09-27 36 views
0

我有以下代码,但我不知道什么是关于循环收集并将每个项目添加到Varray的最佳实践。PL/SQL循环收集并添加到另一个集合查询

下面是我当前的代码:

DECLARE 
TYPE student_arraytype IS TABLE OF student%ROWTYPE INDEX BY PLS_INTEGER; 
student_array student_arraytype; 

--varray type 
TYPE student_varraytype IS VARRAY(255) OF NUMBER; 
--use of the type 
student_varray student_varraytype; 
BEGIN 
    SELECT student_id 
    BULK COLLECT INTO student_array 
    FROM student 
    WHERE student_class = 10; 
-- 
FOR i IN student_array.FIRST .. student_array.LAST 
    LOOP 
    student_varray(i) := i; 
    END LOOP; 
END; 

这是通过表收集循环并增加了VARRAY的正确方法是什么?

如果有人知道替代我欣赏,如果你可以与我分享意见。

亲切的问候,

+0

你真的需要将记录添加到VARRAY吗? – Ollie

+0

你能解释一下你试图完成什么,因为你的代码没什么意义吗?使用嵌套表集合有什么问题('type student_list是学生%rowtype;'的表)?一般来说,最好的做法是不循环(但有些情况下无法避免)。 – user272735

回答

0

可以批量聚集成多个集合:

SELECT student_id, rownum 
    BULK COLLECT INTO student_array, student_varray 
    FROM student 
    WHERE student_class = 10; 

而且在你的榜样,你不要的项目添加到VARRAY关联数组中,该项目只是指数。

相关问题