I donβt think there is a built-in function that is looking for a collection. However, if you know that you will need to search the collection a lot, you can create an index. Adding an item to the collection will be a little more expensive, but the item will be searched for O (1) (instead of O (n) to search for brute force). For example, you can use something like this:
SQL> DECLARE 2 TYPE t_test IS TABLE OF VARCHAR2(1); 3 TYPE t_test_r IS TABLE OF NUMBER INDEX BY VARCHAR2(1); 4 5 v_test t_test; 6 v_test_r t_test_r; 7 8 FUNCTION get_index(p_test_r t_test_r, 9 p_element VARCHAR2) RETURN NUMBER IS 10 BEGIN 11 RETURN p_test_r(p_element); 12 EXCEPTION 13 WHEN no_data_found THEN 14 RETURN NULL; 15 END get_index; 16 17 PROCEDURE add_element(p_test IN OUT t_test, 18 p_test_r IN OUT t_test_r, 19 p_element VARCHAR2) IS 20 BEGIN 21 p_test.extend; 22 p_test(p_test.count) := p_element; 23 p_test_r(p_element) := least(p_test.count, 24 nvl(get_index(p_test_r, p_element), 25 p_test.count)); 26 END add_element; 27 BEGIN 28 v_test := NEW t_test(); 29 add_element(v_test, v_test_r, 'A'); 30 add_element(v_test, v_test_r, 'B'); 31 add_element(v_test, v_test_r, 'A'); 32 dbms_output.put_line('A: ' || get_index(v_test_r, 'A')); 33 dbms_output.put_line('B: ' || get_index(v_test_r, 'B')); 34 dbms_output.put_line('C: ' || get_index(v_test_r, 'C')); 35 END; 36 / A: 1 B: 2 C: PL/SQL procedure successfully completed
You can also define a record containing both arrays, and all functions / procedures for interacting with arrays will use this type of record.
Vincent malgrat
source share