Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Filter Collection

MuzzNov 5 2013 — edited Nov 7 2013

Hi All

Need your advice in one of the scenario, I have created 2 collection with same structure and move 1 collection data into other collection by using some condition and I'm wondering that if someone verify my logic or suggest me some better solution. I tried to convert  my scenario by using EMP table. Please find below my code and appreciate your replies. Thanks

Code

SET serveroutput ON

DECLARE

TYPE c_emp IS TABLE OF emp%rowtype;

TYPE c_emp_filter IS TABLE OF emp%rowtype;

v_emp c_emp;

v_emp_filter c_emp_filter:=c_emp_filter();

BEGIN

  SELECT * --empno,ename,JOB,mgr,hiredate,sal,comm,deptno

  BULK COLLECT

  INTO v_emp

  FROM emp;

    BEGIN

  --Transfer v_emp collection elements into v_emp_filter but only deptno =10

    FOR i IN 1..v_emp.count loop

      IF v_emp(i).deptno IN (10) THEN

      v_emp_filter.EXTEND(1);

      v_emp_filter(v_emp_filter.LAST):=v_emp(i);

      END IF;

    END loop;

        BEGIN

          FOR j IN 1..v_emp_filter.count loop

          dbms_output.put_line(v_emp_filter(j).empno);

          END loop;

        END;

     END;

END;

---------------

First I move all data into my 1st collection (v_emp) and then base on that data I filter it into my other collection v_emp_filter. The above codes are working fine but if someone suggest me some efficient method to filter data into collection then I really appreciate it. Thanks

Regards

Shumail

This post has been answered by gaverill on Nov 6 2013
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 5 2013
Added on Nov 5 2013
16 comments
2,571 views