是否有内置函数来确定PL / sql集合中元素的(第一个)索引?
就像是
DECLARE TYPE t_test IS TABLE OF VARCHAR2(1); v_test t_test; BEGIN v_test := NEW t_test('A','B','A'); dbms_output.put_line( 'A: ' || get_index( v_test,'A' ) ); dbms_output.put_line( 'B: ' || get_index( v_test,'B' ) ); dbms_output.put_line( 'C: ' || get_index( v_test,'C' ) ); END;
A: 1 B: 2 C:
无论什么必要,我都可以使用关联数组,嵌套表或变量.如果同一元素存在多次,则第一次出现的索引就足够了.
否则我必须做类似的事情
CREATE FUNCTION get_index ( in_test IN t_test,in_value IN VARCHAR2 ) RETURN PLS_INTEGER AS i PLS_INTEGER; BEGIN i := in_test.FIRST; WHILE( i IS NOT NULL ) LOOP IF( in_test(i) = in_value ) THEN RETURN i; END IF; i := in_test.NEXT(i); END LOOP; RETURN NULL; END get_index;
不确定,如果这真的有帮助,或者你认为它更优雅:
原文链接:https://www.f2er.com/oracle/204833.htmlcreate type t_test as table of varchar2(1); / DECLARE --TYPE t_test IS TABLE OF VARCHAR2(1); v_test t_test; function get_index(q in t_test,c in varchar2) return number is ind number; begin select min(rn) into ind from ( select column_value cv,rownum rn from table(q) ) where cv = c; return ind; end get_index; BEGIN v_test := NEW t_test('A','A'); dbms_output.put_line( 'A: ' || get_index( v_test,'C' ) ); END; / show errors drop type t_test;