从五个不同的Oracle表中获取XML

时间:2013-07-18 12:40:41

标签: xml oracle

我需要得到像

这样的xml
<dataset code="123" title="" pubcode="456" minrows="0">
    <schema code="s1" /> <!-- can be one -->
    <rowset code="rs1" /> <!-- can be one -->
    <sorter>
        <!-- field can be MORE than one -->
        <field name="field1" order="o1"/>
        <field name="field2" order="o2"/>
    </sorter>
    <!-- filter can be MORE than one -->
    <filter type="filter1" value="val1" />
    <filter type="filter2" value="val2" />
</dataset>

每个标签对应一个单独的表。 并且该标记中的每个属性都是相应表中的一列 在下面写了相同的SQL

 SELECT  XMLELEMENT(NAME "dataset",
                       XMLAttributes(ds.DataSet_Code AS "code",ds.DataSet_Title as "title",ds.pub_code as "pubcode",ds.Min_Rows as "minrowss"),
                       XMLFOREST(  
                            (SELECT XMLElement("schema", XMLAttributes(fs.schema_code AS "code"))
                            FROM File_Schema fs WHERE fr.dataset_code = ds.dataset_code),
                            (SELECT XMLElement("rowset", XMLAttributes(fr.rowset_code AS "code")) FROM File_RowSet fr
                              WHERE fr.dataset_code = ds.dataset_code),
                            (SELECT XMLELEMENT(NAME "sorter",
                                        XMLAGG(XMLELEMENT(NAME "field",
                                                            XMLATTRIBUTES(fsf.field_name AS "name",fsf.field_order AS "order")
                                                          )
                                                )
                                        )
                            FROM File_sorter_field fsf WHERE fsf.dataset_code=ds.dataset_code),
                            (SELECT XMLAGG(XMLELEMENT(NAME "filter", XMLATTRIBUTES(type AS "type",value AS "value")))
                                        FROM File_Filter ff where ff.dataset_code=ds.dataset_code)

                    ))
FROM File_Product fp , File_DataSet ds
WHERE fp.File_Name = ds.File_Name and fp.File_Name = 'abc' and ds.dataset_code ='123' ;

我得到的错误如下

    ORA-19208: parameter 1 of function XMLFOREST must be aliased 19208. 00000 
  - "parameter %s of function %s must be aliased" *Cause: The indicated parameter 
  of the XML generation function has not been aliased, although it is an expression.
   *Action: Specify an alias for the expression using the AS clause. 
   Error at Line: 19 Column: 5

任何帮助表示赞赏

根据KPater87修改后的查询

SELECT  XMLELEMENT(NAME "dataset",
                       XMLAttributes(ds.DataSet_Code AS "code",ds.DataSet_Title as "title",ds.pub_code as "pubcode",ds.Min_Rows as "minrowss"),
                       XMLConcat(  
                            SELECT XMLElement("schema", XMLAttributes(fs.schema_code AS "code"))
                            FROM File_Schema fs WHERE fs.dataset_code = ds.dataset_code,
                            SELECT XMLElement("rowset", XMLAttributes(fr.rowset_code AS "code")) FROM File_RowSet fr
                              WHERE fr.dataset_code = ds.dataset_code,
                            SELECT XMLELEMENT(NAME "sorter",
                                        XMLAGG(XMLELEMENT(NAME "field",
                                                            XMLATTRIBUTES(fsf.field_name AS "name",fsf.field_order AS "order")
                                                          )
                                                )
                                        )
                            FROM File_sorter_field fsf WHERE fsf.dataset_code=ds.dataset_code,
                            SELECT XMLAGG(XMLELEMENT(NAME "filter", XMLATTRIBUTES(type AS "type",value AS "value")))
                                        FROM File_Filter ff where ff.dataset_code=ds.dataset_code 

                    ))
FROM File_DataSet ds
WHERE ds.File_Name = 'abc' and ds.dataset_code ='123' ;

仍然出错

ORA-00936:表达式缺失 00936. 00000 - “缺少表达” *原因:
*行动: 行错误:4列:28

2 个答案:

答案 0 :(得分:3)

我认为问题在于第16行

FROM File_Filter ff where ff.dataset_code=ds.dataset_code) ,

最后的逗号意味着Oracle之前正在期待另一个表达式 第18行。)

答案 1 :(得分:0)

代码中的问题:

  1. 正如@stevo所写:不必要的逗号
  2. XMLForest段中SELECT内的WHEREXMLForest部分错误别名
  3. 您不应该使用SELECT,而应使用XMLConcat
  4. 在主WHERE中加入两个表是不必要的。经过小幅修改后:将WHERE ds.File_Name = 'abc' and ds.dataset_code ='123'部分更改为File_Product表格{{1}}无处可用。