Oracle
 sql >> Database >  >> RDS >> Oracle

Come posso restituire un elenco di valori anziché una stringa quando si esegue una query su un database Oracle utilizzando XPath?

EXTRACT (e EXTRACTVALUE ) sono funzioni obsolete. Dovresti usare XMLTABLE invece:

with sample_data as (select xmltype('<film>
    <title>Godfather, The</title>
    <year>1972</year>
    <directors>
        <director>Francis Ford Coppola</director>
    </directors>
    <genres>
        <genre>Crime</genre>
        <genre>Drama</genre>
    </genres>
    <plot>Son of a mafia boss takes over when his father is critically wounded in a mob hit.</plot>
    <cast>
        <performer>
            <actor>Marlon Brando</actor>
            <role>Don Vito Corleone</role>
        </performer>
        <performer>
            <actor>Al Pacino</actor>
            <role>Michael Corleone</role>
        </performer>
        <performer>
            <actor>Diane Keaton</actor>
            <role>Kay Adams Corleone</role>
        </performer>
        <performer>
            <actor>Robert Duvall</actor>
            <role>Tom Hagen</role>
        </performer>
        <performer>
            <actor>James Caan</actor>
            <role>Sonny Corleone</role>
        </performer>
    </cast>
</film>') x from dual)
select x.*
from   sample_data sd,
       xmltable('/film[title="Godfather, The"]/cast/performer' passing sd.x
                columns actor varchar2(50) path '//actor',
                        role varchar2(50) path '//role') x;

ACTOR                                              ROLE                                              
-------------------------------------------------- --------------------------------------------------
Marlon Brando                                      Don Vito Corleone                                 
Al Pacino                                          Michael Corleone                                  
Diane Keaton                                       Kay Adams Corleone                                
Robert Duvall                                      Tom Hagen                                         
James Caan                                         Sonny Corleone  

(Ho incluso la colonna del ruolo solo per ulteriori informazioni; rimuoveresti semplicemente quella colonna dall'elenco delle colonne in XMLTABLE parte se non hai bisogno di vederlo.)