ci sono diverse opzioni. Vedere Split single comma delimited string into rows in Oracle
Hai solo bisogno di aggiungere LIVELLO nell'elenco di selezione come una colonna, per ottenere la sequenza di numero ad ogni riga restituita. Oppure, ROWNUM sarebbe anche sufficiente.
Utilizzando una delle seguenti SQL, è possibile includerle in una FUNZIONE .
INSTR in CONNECT BY clausola:
SQL> WITH DATA AS
2 (SELECT 'word1, word2, word3, word4, word5, word6' str FROM dual
3 )
4 SELECT trim(regexp_substr(str, '[^,]+', 1, LEVEL)) str
5 FROM DATA
6 CONNECT BY instr(str, ',', 1, LEVEL - 1) > 0
7/
STR
----------------------------------------
word1
word2
word3
word4
word5
word6
6 rows selected.
SQL>
REGEXP_SUBSTR in CONNECT BY clausola:
SQL> WITH DATA AS
2 (SELECT 'word1, word2, word3, word4, word5, word6' str FROM dual
3 )
4 SELECT trim(regexp_substr(str, '[^,]+', 1, LEVEL)) str
5 FROM DATA
6 CONNECT BY regexp_substr(str , '[^,]+', 1, LEVEL) IS NOT NULL
7/
STR
----------------------------------------
word1
word2
word3
word4
word5
word6
6 rows selected.
SQL>
REGEXP_COUNT in CONNECT BY clausola:
SQL> WITH DATA AS
2 (SELECT 'word1, word2, word3, word4, word5, word6' str FROM dual
3 )
4 SELECT trim(regexp_substr(str, '[^,]+', 1, LEVEL)) str
5 FROM DATA
6 CONNECT BY LEVEL
Utilizzando XMLTABLE
SQL> WITH DATA AS
2 (SELECT 'word1, word2, word3, word4, word5, word6' str FROM dual
3 )
4 SELECT trim(COLUMN_VALUE) str
5 FROM DATA, xmltable(('"' || REPLACE(str, ',', '","') || '"'))
6/
STR
------------------------------------------------------------------------
word1
word2
word3
word4
word5
word6
6 rows selected.
SQL>
Utilizzando MODELLO clausola:
SQL> WITH t AS
2 (
3 SELECT 'word1, word2, word3, word4, word5, word6' str
4 FROM dual) ,
5 model_param AS
6 (
7 SELECT str AS orig_str ,
8 ','
9 || str
10 || ',' AS mod_str ,
11 1 AS start_pos ,
12 Length(str) AS end_pos ,
13 (Length(str) - Length(Replace(str, ','))) + 1 AS element_count ,
14 0 AS element_no ,
15 ROWNUM AS rn
16 FROM t)
17 SELECT trim(Substr(mod_str, start_pos, end_pos-start_pos)) str
18 FROM (
19 SELECT *
20 FROM model_param MODEL PARTITION BY (rn, orig_str, mod_str)
21 DIMENSION BY (element_no)
22 MEASURES (start_pos, end_pos, element_count)
23 RULES ITERATE (2000)
24 UNTIL (ITERATION_NUMBER+1 = element_count[0])
25 (start_pos[ITERATION_NUMBER+1] = instr(cv(mod_str), ',', 1, cv(element_no)) + 1,
26 end_pos[iteration_number+1] = instr(cv(mod_str), ',', 1, cv(element_no) + 1)))
27 WHERE element_no != 0
28 ORDER BY mod_str ,
29 element_no
30/
STR
------------------------------------------
word1
word2
word3
word4
word5
word6
6 rows selected.
SQL>
Si potrebbe anche usare DBMS_UTILITY pacchetto fornito da Oracle. Fornisce vari sottoprogrammi di utilità. Un'utilità utile è la procedura COMMA_TO_TABLE, che converte un elenco di nomi delimitato da virgole in una tabella di nomi PL/SQL.
Leggi DBMS_UTILITY.COMMA_TO_TABLE
Vedere [** Dividere la stringa delimitata da una virgola singola in righe in Oracle **] (https://lalitkumarb.com/2014/12/02/split-comma-delimited-string-into-rows-in-oracle/) –
Sopra il collegamento corretto, vedere [** Dividere la stringa delimitata da una virgola singola in righe in Oracle **] (https://lalitkumarb.wordpress.com/2014/12/02/split-comma-delimited-string-into- rows-in-oracle /) –