在以下SQL示例中,如何在没有别名的情况下使用XMLAgg函数?:

时间:2013-09-30 13:05:59

标签: sql xml oracle11g

我必须制作符合我无法控制的格式的XML。它由一个名为DateRange的标题组成,然后是查询中的记录。我可以生成几乎与格式匹配的XML,除了我的版本在元素中有记录,在这个例子中是DIV:

select
    XMLRoot(
        XMLElement(
            "PayrollAdjustments",
                XMLForest(
                    XMLForest(
                        '2013-09-15' as "From",
                        '2013-09-21' as "To"                    
                    ) as "DateRange",
                    XMLAgg(                  
                        XMLForest(
                            XMLForest(
                                EMPLOYEE as "EmployeeId",
                                STORE AS "StoreNumber",
                                DEPARTMENT AS "Department",
                                WORKCODE AS "AdjustCode",
                                PAYROLL_DATE AS "PayDate",
                                HOURS as "Hours",
                                0 as "Amount"
                            ) AS "PayAdjustment"
                        )   
                    ) AS div
                )
            ),
            VERSION '1.0" encoding="utf-8',
            STANDALONE YES
        ).getClobVal()
from timecard_payroll
where start_date = '2013-09-15'
and end_date = '2013-09-21'
and hours > 0;

这是输出的样子,因为你可以看到记录在一个名为DIV的元素中:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<PayrollAdjustments>
    <DateRange>
        <From>2013-09-15</From>
        <To>2013-09-21</To>
    </DateRange>
    <DIV>
        <PayAdjustment>
            <EmployeeId>262699</EmployeeId>
            <StoreNumber>8159</StoreNumber>
            <Department>1</Department>
            <AdjustCode>91</PayAdjustCode>
            <PayDate>2013-09-16</PayDate>
            <Hours>8.0000</Hours>
            <Amount>0</Amount>
        </PayAdjustment>
        <PayAdjustment>
            <EmployeeId>262916</EmployeeId>
            <StoreNumber>8294</StoreNumber>
            <Department>5</Department>
            <AdjustCode>91</AdjustCode>
            <PayDate>2013-09-19</PayDate>
            <Hours>8.0000</Hours>
            <Amount>0</Amount>
        </PayAdjustment>
    </DIV>  
</PayrollAdjustments>

如何更改我的查询以摆脱DIV元素,使其看起来像这样?:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<PayrollAdjustments>
    <DateRange>
        <From>2013-09-15</From>
        <To>2013-09-21</To>
    </DateRange>        
    <PayAdjustment>
        <EmployeeId>262699</EmployeeId>
        <StoreNumber>8159</StoreNumber>
        <Department>1</Department>
        <AdjustCode>91</PayAdjustCode>
        <PayDate>2013-09-16</PayDate>
        <Hours>8.0000</Hours>
        <Amount>0</Amount>
    </PayAdjustment>
    <PayAdjustment>
        <EmployeeId>262916</EmployeeId>
        <StoreNumber>8294</StoreNumber>
        <Department>5</Department>
        <AdjustCode>91</AdjustCode>
        <PayDate>2013-09-19</PayDate>
        <Hours>8.0000</Hours>
        <Amount>0</Amount>
    </PayAdjustment>          
</PayrollAdjustments>

提前致谢

1 个答案:

答案 0 :(得分:1)

XMLConcat结合使用XMLSequenceType

select
    XMLRoot(
        XMLElement(
            "PayrollAdjustments",
                XMLForest(
                    XMLForest(
                        '2013-09-15' as "From",
                        '2013-09-21' as "To"                    
                    ) as "DateRange",
                    XMLConcat(XMLSequenceType(XMLAgg(XMLElement("PayAdjustment",
                            XMLForest(
                                EMPLOYEE as "EmployeeId",
                                STORE AS "StoreNumber",
                                DEPARTMENT AS "Department",
                                WORKCODE AS "AdjustCode",
                                PAYROLL_DATE AS "PayDate",
                                HOURS as "Hours",
                                0 as "Amount"
                            ) AS "PayAdjustment"
                    )))
                )
            ),
            VERSION '1.0" encoding="utf-8',
            STANDALONE YES
        ).getClobVal()
from timecard_payroll
where start_date = '2013-09-15'
and end_date = '2013-09-21'
and hours > 0;