在pl/sql中使用xmlelement获取所有关系表数据 [英] get all relational tables data using xmlelement in pl/sql

查看:446
本文介绍了在pl/sql中使用xmlelement获取所有关系表数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取所有连接的表数据,同时以xml格式获取值

I need to get all the connected tables data while getting values as xml

到目前为止,我可以生成如下特定的表格结果,

Till now I can produce specific table results as below,

表1

id  name    rollNo
1   aaa     10
2   bbb     15

表2

id  rollNo  div
1   10       a
2   15       b

查询

SELECT XMLElement("table1", 
XMLAttributes(t.id, t.name, t.rollno))
AS "RESULT"
FROM table1 t where t.rollno=15

结果

<table1 id="2" name="bbb" rollno="15"></table1>

但是这样,我只能获取特定表的数据. 我想通过外键获取与父表相关的所有表的xml数据.

But this way, I am able to get data for specific table only. I want to get the xml data for all the tables related with parent table by foreign key.

假设,这里table3与table1相关或table 4与表3相关,这也应该出现在生成的xml中.基本上,我正在寻找完整的树.

Suppose, here table3 is related with table1 or table 4 is related with table 3, That should also come in generated xml. Basically I am looking for full tree.

预期结果:

<table1 id="2" name="bbb" rollno="15"></table1>
<table2 id="2" rollno="15" div="b"></table2>
<table3.. and so on

推荐答案

您可以创建一个函数,该函数组合数据字典查询以查找父子关系,并DBMS_XMLGEN.GETXML生成并组合XML.

You can create a function that combines data dictionary queries to find parent-child relationships and DBMS_XMLGEN.GETXML to generate and combine XML.

下面的函数使用ROWID,查找父表和子表,并为相关行生成XML.这里有许多假设,要处理真实数据可能要花费大量工作.

The below function takes a ROWID, finds the parent and child table, and generates XML for the relevant rows. There are many assumptions here, and this may take a huge amount of work to get working with real data.

create or replace function get_related_xml(p_rowid rowid) return xmltype is
    v_child_table_owner varchar2(128);
    v_child_table_name varchar2(128);

    v_parent_table_owner varchar2(128);
    v_parent_table_name varchar2(128);
    v_column varchar2(128);

    v_child_xml xmltype;
    v_parent_xml xmltype;
    v_combined_xml xmltype;
begin
    --Get child table directly referenced by ROWID.
    select owner, object_name
    into v_child_table_owner, v_child_table_name
    from all_objects
    where object_type = 'TABLE'
        and object_id = dbms_rowid.rowid_object(p_rowid);

    --Get parent table based on child table ROWID, and join columns.
    --(ASSUMPTION: Tables only have one column, with the same name, that joins the tables.)
    select owner, table_name, column_name
    into v_parent_table_owner, v_parent_table_name, v_column
    from all_cons_columns
        where (owner, constraint_name) in
        (
            --Foreign key constraints based on the relevant table.
            select r_owner, r_constraint_name
            from all_constraints
            where constraint_type = 'R'
                and (owner, table_name) in
                (
                    --Table referenced by ROWID.
                    select owner, object_name
                    from all_objects
                    where object_type = 'TABLE'
                        and object_id = dbms_rowid.rowid_object(p_rowid)
                )
        );

    --Generate child XML.
    v_child_xml := dbms_xmlgen.getXMLType
        (
            'select c.*
            from '||v_child_table_owner||'.'||v_child_table_name||' c
            join '||v_parent_table_owner||'.'||v_parent_table_name||' p
                on c.'||v_column||' = p.'||v_column||'
            where c.rowid = '''||p_rowid||''''
        );

    --Generate parent XML.
    v_parent_xml := dbms_xmlgen.getXMLType
        (
            'select p.*
            from '||v_child_table_owner||'.'||v_child_table_name||' c
            join '||v_parent_table_owner||'.'||v_parent_table_name||' p
                on c.'||v_column||' = p.'||v_column||'
            where c.rowid = '''||p_rowid||''''
        );

    --Combine the XML and return them.
    select xmlconcat(v_child_xml, v_parent_xml)
    into v_combined_xml
    from dual;

    return v_combined_xml;
end get_related_xml;
/

调用该函数很容易.当前版本未按照您想要的格式返回正确的数据,您可能需要转换XML.

Calling the function is easy. The current version doesn't return data in exactly the format you want, you may need to transform the XML.

select get_related_xml(rowid)
from table1
where rollno=15;

<ROWSET>
 <ROW>
  <ID>2</ID>
  <NAME>bbb</NAME>
  <ROLLNO>15</ROLLNO>
 </ROW>
</ROWSET>
<ROWSET>
 <ROW>
  <ID>2</ID>
  <ROLLNO>15</ROLLNO>
  <DIV>b</DIV>
 </ROW>
</ROWSET>

这是我用来生成上述结果的示例架构.这是一个简单的架构,只有一个父子关系,基于两个表中具有相同名称的单个列.

Here's the sample schema I used to generate the above results. This is a simple schema, with only one parent-child relationship, based on a single column that has the same name in both tables.

create table table2(id number primary key, rollNo number unique, div varchar2(100));
insert into table2
select 1, 10, 'a' from dual union all
select 2, 15, 'b' from dual;

create table table1(id number primary key, name varchar2(100), rollNo number,
    constraint table1_fk foreign key (rollNo) references table2(rollNo));
insert into table1
select 1, 'aaa', 10 from dual union all
select 2, 'bbb', 15 from dual;

这篇关于在pl/sql中使用xmlelement获取所有关系表数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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