2016-09-19 4 views
0

Ich habe eine Tabelle wie diese.PL/SQL aufgeteilt in viele Zeilen

|PARAMKEY | PARAMVALUE ----------+------------ KEY |[["PAR_A",2,"SCH_A"],["PAR_B",4,"SCH_B"],["PAR_C",3,"SCH_C"]]

Ich brauche die Werte in drei Spalten aufgeteilt und ich verwende REGEXP_SUBSTR. Hier ist mein Code.

SELECT REGEXP_SUBSTR(paramvalue, '[^],["]+', 1,1) PARAMETER 
     ,REGEXP_SUBSTR(paramvalue, '[^],[",]+', 1, 2) VERSION 
     ,REGEXP_SUBSTR(paramvalue, '[^],["]+', 1, 3) SCHEMA 
     FROM tmp_param_table 
where paramkey = 'KEY' 
     UNION ALL 
SELECT REGEXP_SUBSTR(paramvalue, '[^],["]+', 1, 4) PARAMETER 
     ,REGEXP_SUBSTR(paramvalue, '[^],[",]+', 1, 5) VERSION 
     ,REGEXP_SUBSTR(paramvalue, '[^],["]+', 1, 6) SCHEMA 
     FROM tmp_param_table 
where paramkey = 'KEY' 
     UNION ALL 
SELECT REGEXP_SUBSTR(paramvalue, '[^],["]+', 1, 7) PARAMETER 
     ,REGEXP_SUBSTR(paramvalue, '[^],[",]+', 1, 8) VERSION 
     ,REGEXP_SUBSTR(paramvalue, '[^],["]+', 1, 9) SCHEMA 
     FROM tmp_param_table 
where paramkey = 'KEY'; 

und das ist das Ergebnis, das ich brauche.

PARAMETER | VERSION | SCHEMA ---------+---------+------- PAR_A |2 |SCH_A PAR_B |4 |SCH_B PAR_C |3 |SCH_C

Aber der Wert ist zu lang und ich hoffe, es ist eine andere Art und Weise es simplier zu machen, indem Schleife oder irgendetwas verwendet wird. Dank

+0

Siehe diese ex. http://stackoverflow.com/documentation/oracle/1968/splitting-delimited-strings –

+0

Wie lange ist die PARAMVALUE Spalte? –

+0

@DmitryGrekov der data_type ist clob und ich muss den Wert in 52 Zeile auswählen. –

Antwort

1

versuchen, etwas wie folgt aus:

with tmp_param_table as 
(
select 'KEY' as PARAMKEY , '[["PAR_A",2,"SCH_A"],["PAR_B",4,"SCH_B"],["PAR_C",3,"SCH_C"]],["PAR_D",4,"SCH_D"]]' as PARAMVALUE from dual 
), 
levels as  (select level as lv from dual connect by level <= 156), 
steps as (select lv-2 as step from levels where MOD(lv,3)=0) 
select step, (SELECT REGEXP_SUBSTR(paramvalue, '[^],["]+',1, step) PARAMETER FROM tmp_param_table where paramkey = 'KEY') parameter, 
      (SELECT REGEXP_SUBSTR(paramvalue, '[^],["]+',1, step+1) PARAMETER FROM tmp_param_table where paramkey = 'KEY') version, 
      (SELECT REGEXP_SUBSTR(paramvalue, '[^],["]+',1, step+2) PARAMETER FROM tmp_param_table where paramkey = 'KEY') schema 
       from steps 

Hier

levels - kehrt Zahlen Form 1 bis 156 (52*3) (oder was auch immer Sie brauchen)

steps - sind die Nummern 1, 4, 7 usw. mit Schritt 3

Ergebnisse:

1 PAR_A 2 SCH_A 
4 PAR_B 4 SCH_B 
7 PAR_C 3 SCH_C 
10 PAR_D 4 SCH_D 
13      
etc.. 
0

ich versucht habe regulären Ausdruck

und ParamValue Spaltenwert Teil in gemeinsamen separierte

SELECT 
REGEXP_SUBSTR(COL, '[^],["]+', 1, 1) PARAMETER, 
REGEXP_SUBSTR(COL, '[^],[",]+', 1, 2) VERSION, 
REGEXP_SUBSTR(COL, '[^],["]+', 1, 3) SCHEMA 
FROM 
(
SELECT paramkey,REGEXP_SUBSTR(to_char(paramvalue),'[^][^]+',1,level) COL 
from tmp_param_table 
connect by regexp_substr(to_char(paramvalue),'[^][^]+',1, level) is not null 
) 
WHERE COL <>',' 

Ich hoffe, dies kann Hilfe.

+0

so nah. Ich muss where-Klausel entfernen, weil ich Fehler ORA-00932 bekomme: inkonsistente Datentypen: erwartet - bekam CLOB 00932. 00000 - "inkonsistente Datentypen: erwartete% s hat% s" * Ursache: * Aktion: Fehler bei Zeile: 11 Spalte: 7 –

+0

Hier habe ich einige reguläre Ausdrücke verpasst, also zwei zusätzliche Zeilen für das Komma. –

+0

Ändern Paramvalue zu ** to_char (paramvalue) ** in Zeile 7 und 9. siehe ich habe bearbeitet wie du brauchst. –