如何在oracle中使用xmltable? [英] how to use xmltable in oracle?

查看:374
本文介绍了如何在oracle中使用xmltable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Oracle的XML DB创建用户配置文件.我将用户配置文件与表中的其他关系列(ID,用户名,密码)存储在单个XMLTYPE列中. XML具有以下格式:

I am using Oracle's XML DB to create user profiles. I stored user profiles in a single XMLTYPE column with other relational columns (id, username, password) in the table. The XML is of the following format:

<profile>
<subject>I
       <action>like
           <object>sports</object>
               ...
           <object>music</object
       </action>
    </subject>
</profile>

我使用了以下查询,

SELECT *
FROM user,
XMLTABLE(
 '//profile'
 PASSING user.profile
 return COLUMNS action VARCHAR2(20) PATH '/subject/action',
         object VARCHAR2(30) PATH '/subject/action/object'
);

这什么也没给我.我怎样才能使这个东西正常工作?

which gives me nothing. How could I make this thing work?

推荐答案

actionobject不在同一级别,因此查询必须执行其他步骤.这是一个示例:

action and object on your example aren't on the same level, so your query has to perform additional steps. Here's an example:

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>

如您所见,我们得到了该节点,而不是您真正想要的节点,因此我们添加了XMLTABLE:

As you can see we got the node, not really what you want so we add an XMLTABLE:

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

这篇关于如何在oracle中使用xmltable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆