4
我想通过在PLSQL函数中为oracle使用一个查询来获得嵌套的XML。Oracle SQL生成嵌套的xml
数据库(不能更改):
table 'products_details'
:
`attr_id` | `attribute` | `fk_parent_id`(Foreign key on `attr_id`)
-------------------------------------------------------------------------------
1 | name | null
3 | sizes | null
4 | size_women | 3
5 | size_man | 3
6 | size_dimension | 3
table 'product_contents'
:
`detail` | `value` | variation_number | `product_id` (doesnt matter)
-------------------------------------------------------------------------------
name | Tshirt | null | 1000
price | 14.99 | null | 1000
size_man | XL | 1 | 1000
size_women | L | 1 | 1000
size_dimesion | 21x25cm | 1 | 1000
size_man | M | 2 | 1000
size_women | S | 2 | 1000
size_dimesion | 14x16cm | 2 | 1000
...
正如你可以看到有一些选项(名称,价格),只有一次每个产品,但也有一些选项(size_man,size_woman ...)是变化,并可能存在多个时间为每个产品。
我要的是一个XML:
<attribute detail="name">Tshirt</attribute>
<attribute detail="price">14.99</attribute>
<attribute detail="sizes">
<row variation_number="1">
<attribute detail="size_man">XL</attribute>
<attribute detail="size_women">L</attribute>
...
</row>
<row variation_number="2">
<attribute detail="size_man">M</attribute>
<attribute detail="size_women">S</attribute>
</row>
</attribute>
我试了一下,到目前为止(这当然不是真正的工作):
SELECT
(
XMLELEMENT( "attribute",
XMLATTRIBUTES(pc.detail as "detail"),
(SELECT XMLAGG
(
XMLELEMENT("row", XMLATTRIBUTES(pc.variant_number as "variation_number"))
)
FROM product_contents pc
JOIN product_details pd ON pc.detail = pd.attribute and pc.product_id = '1000'
WHERE pd.fk_parent_id = pd.ID
)
).getClobVal() CONTENT
FROM product_details pd
pd.fk_parent_id is null
order by pd.attribute;
我怎样才能做到这只是一个单一查询?
这从你的表设置没有意义:“pc.identifier = pc.detail”也不计算:“pd.fk_parent_id = pd.ID”。你在这里显示的DDL表中缺少信息吗? – tbone
我修复了“pc.identifier = pc.detail”部分。查询只是一个尝试,并不像我说的那样工作。 TIA为您提供帮助 – frgtv10