问题
I need to get all the connected tables data while getting values as xml
Till now I can produce specific table results as below,
Table1
id name rollNo
1 aaa 10
2 bbb 15
Table2
id rollNo div
1 10 a
2 15 b
Query
SELECT XMLElement("table1",
XMLAttributes(t.id, t.name, t.rollno))
AS "RESULT"
FROM table1 t where t.rollno=15
Result
<table1 id="2" name="bbb" rollno="15"></table1>
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.
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.
Expected result:
<table1 id="2" name="bbb" rollno="15"></table1>
<table2 id="2" rollno="15" div="b"></table2>
<table3.. and so on
回答1:
You can create a function that combines data dictionary queries to find parent-child relationships and DBMS_XMLGEN.GETXML
to generate and combine 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;
/
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;
来源:https://stackoverflow.com/questions/55283761/get-all-relational-tables-data-using-xmlelement-in-pl-sql