SQL >> Database >  >> RDS >> Oracle

XMLTABLE gebruiken in Oracle

action en object staan in je voorbeeld niet op hetzelfde niveau, dus je query moet extra stappen uitvoeren. Een voorbeeld:

SQL> create table users (id number, profile xmltype); Table created. SQL> insert into users values (1, XMLTYPE('<profile> 2 <subject>I 3 <action>like 4 <object>sports</object> 5 <object>music</object> 6 </action> 7 </subject> 8 </profile>')); 1 row created. SQL> select u.id, x.action, x.object.getStringVal() 2 from users u, 3 XMLTABLE('/profile/subject/action' 4 passing u.profile 5 columns action VARCHAR2(30) PATH 'text()', 6 object XMLTYPE PATH 'object') x; ID ACTION X.OBJECT.GETSTRINGVAL() --- ------- -------------------------------------------------- 1 like <object>sports</object> <object>music</object>

Zoals je ziet krijgen we de node, niet echt wat je wilt, dus voegen we nog een XMLTABLE toe:

SQL> select u.id, x.action, y.object 2 from users u, 3 XMLTABLE('/profile/subject/action' 4 passing u.profile 5 columns action VARCHAR2(30) PATH 'text()', 6 object XMLTYPE PATH 'object') x, 7 XMLTABLE('/object' 8 passing x.object 9 columns object VARCHAR2(30) PATH '.') y; ID ACTION OBJECT --- ------- ------- 1 like sports 1 like music
  1. Een variabele gebruiken in XPath met Oracle XMLTable

  2. XML niet direct in OCI-Lob-object omzetten in PHP

  3. Savepoints gebruiken in een Oracle-procedure

  4. Waarom Oracle geen cascade-updates op primary keys ondersteunt