plsql
Collecties en records
Zoeken…
Gebruik een verzameling als retourtype voor een splitfunctie
Het is noodzakelijk om het type aan te geven; hier t_my_list
; een verzameling is een TABLE OF
something
CREATE OR REPLACE TYPE t_my_list AS TABLE OF VARCHAR2(100);
Dit is de functie. Let op de ()
gebruikt als een soort constructor, en de trefwoorden COUNT
en EXTEND
die u helpen bij het maken en uitbreiden van uw verzameling;
CREATE OR REPLACE
FUNCTION cto_table(p_sep in Varchar2, p_list IN VARCHAR2)
RETURN t_my_list
AS
--- this function takes a string list, element being separated by p_sep
-- as separator
l_string VARCHAR2(4000) := p_list || p_sep;
l_sep_index PLS_INTEGER;
l_index PLS_INTEGER := 1;
l_tab t_my_list := t_my_list();
BEGIN
LOOP
l_sep_index := INSTR(l_string, p_sep, l_index);
EXIT
WHEN l_sep_index = 0;
l_tab.EXTEND;
l_tab(l_tab.COUNT) := TRIM(SUBSTR(l_string,l_index,l_sep_index - l_index));
l_index := l_sep_index + 1;
END LOOP;
RETURN l_tab;
END cto_table;
/
Vervolgens kunt u de inhoud van de verzameling bekijken met de functie TABLE()
van SQL; het kan worden gebruikt als een lijst in een SQL IN ( ..)
-instructie:
select * from A_TABLE
where A_COLUMN in ( TABLE(cto_table('|','a|b|c|d')) )
--- gives the records where A_COLUMN in ('a', 'b', 'c', 'd') --
Modified text is an extract of the original Stack Overflow Documentation
Licentie onder CC BY-SA 3.0
Niet aangesloten bij Stack Overflow