如何使用PL SQL输出此XML格式

时间:2016-02-04 09:34:49

标签: xml plsql

我想编写一个pl sql函数,它以下面的xml格式返回oracle表数据:

<loans>
<loan>
<id>1</id>
<name>Bank Loan</name>
<description>Bank Loan-1</description>
<amount>25000</amount>
<sanction_date>2016-01-27</sanction_date>
<rate>4.5</rate>
</loan>

<loan>
<id>2</id>
<name>Bank 2</name>
<description>Bank Loan-2</description>
<amount>452500</amount>
<sanction_date>2016-01-27</sanction_date>
<rate>7.67</rate>
</loan>

<loan>
<id>3</id>
<name>New Loan</name>
<description>NY Bank Loan</description>
<amount>1250000</amount>
<sanction_date>2016-01-27
</sanction_date>
<rate>12.25</rate>
</loan>
</loans>

但不是上面的格式,它给了我以下格式的许多贷款标签,而不是一个作为父母:

<loans>
<loan>
<id>1</id>
<name>Bank Loan</name>
<description>Bank Loan-1</description>
<amount>25000</amount>
<sanction_date>2016-01-27</sanction_date>
<rate>4.5</rate>
</loan>
</loans>

<loans>
<loan>
<id>2</id>
<name>Bank 2</name>
<description>Bank Loan-2</description>
<amount>452500</amount>
<sanction_date>2016-01-27</sanction_date>
<rate>7.67</rate>
</loan>
</loans>

<loans>
<loan>
<id>3</id>
<name>New Loan</name>
<description>NY Bank Loan</description>
<amount>1250000</amount>
<sanction_date>2016-01-27
</sanction_date>
<rate>12.25</rate>
</loan>
</loans>

作为初学者,我认为我以错误的方式安排它们。这是我的PL SQL代码:

SELECT xmlagg
             (
              xmlelement("loans",                                        
                    XMLAGG(
                        xmlelement("loan",
                           xmlelement("id", e.LNTYPE_ID), 
                           xmlelement("name", e.LNTYPE_NAME), 
                           xmlelement("description", e.LNTYPE_DESCRIPTION),
                           xmlelement("amount", e.LNTYPE_AMOUNT),
                           xmlelement("sanction_date", e.LNTYPE_SANCTION_DATE),
                           xmlelement("rate", e.LNTYPE_INTEREST_RATE)
                           )
                        )
                       )
              )
        as clob 
    FROM TBL_LOAN_TYPE e
    group by e.LNTYPE_ID;

1 个答案:

答案 0 :(得分:1)

你很亲密,你只需要摆脱外面的xmlagg和小组:

SELECT xmlelement("loans",                                        
         XMLAGG(
           xmlelement("loan",
             xmlelement("id", e.LNTYPE_ID), 
             xmlelement("name", e.LNTYPE_NAME), 
             xmlelement("description", e.LNTYPE_DESCRIPTION),
             xmlelement("amount", e.LNTYPE_AMOUNT),
             xmlelement("sanction_date", e.LNTYPE_SANCTION_DATE),
             xmlelement("rate", e.LNTYPE_INTEREST_RATE)
          )
        )
      ) as clob 
FROM TBL_LOAN_TYPE e;

编辑:当您在e.LNTYPE_ID上使用group by时,实际上会获得三个单独的行,因为这些ID都是唯一的。例如,运行此代码:

SELECT xmlelement("loans",                                        
         XMLAGG(
           xmlelement("loan",
             xmlelement("id", e.LNTYPE_ID), 
             xmlelement("name", e.LNTYPE_NAME), 
             xmlelement("description", e.LNTYPE_DESCRIPTION),
             xmlelement("amount", e.LNTYPE_AMOUNT),
             xmlelement("sanction_date", e.LNTYPE_SANCTION_DATE),
             xmlelement("rate", e.LNTYPE_INTEREST_RATE)
          )
        )
      ) as clob 
FROM TBL_LOAN_TYPE e
GROUP BY e.LNTYPE_ID;

将返回三个独立的XML片段,每个片段用于每笔贷款。外部XMLAGG然后将这些组合成一个XML文档,您可以在此处阅读有关XMLAGG的更多信息:https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions215.htm