ORA-02287:在使用XMLAGG创建Oracle Xml时,此处不允许使用序列号

oxalkeyp  于 2023-05-28  发布在  Oracle
关注(0)|答案(1)|浏览(151)

我正在尝试在Oracle中创建一个XML文档。它应该在顶层有一个序列值,还有一些行级项目。例如,查询在没有行项目的情况下工作

WITH j AS (
  SELECT
    'abc' test
  FROM
    dual
  GROUP BY
    1
)
SELECT
  XMLSERIALIZE(DOCUMENT XMLELEMENT(
    "WhseSnapshot",
    XMLELEMENT(
      "mdDC",
      MD_EXTRACT_ID_SEQ.NEXTVAL
    )
  ))
FROM
  j;

产生输出

<WhseSnapshot><mdDC>157</mdDC></WhseSnapshot>

向查询添加行项目,如下所示

WITH j AS (
  SELECT
    'abc' test
  FROM
    dual
  GROUP BY
    1
)
SELECT
  XMLSERIALIZE(DOCUMENT XMLELEMENT(
    "WhseSnapshot",
    XMLELEMENT(
      "mdDC",
      MD_EXTRACT_ID_SEQ.NEXTVAL
    ),
    XMLAGG(XMLELEMENT(
      "line",
           XMLFOREST(j.test AS "test")
    ))
  ))
FROM
  j;

导致以下错误

ORA-02287: sequence number not allowed here
02287. 00000 -  "sequence number not allowed here"
*Cause:    The specified sequence number (CURRVAL or NEXTVAL) is inappropriate
           here in the statement.
*Action:   Remove the sequence number.
Error at Line: 84 Column: 25

有没有人能达到类似的目标?我希望避免PL/SQL块或函数,并尽可能坚持使用常规SQL。

4xrmg8kj

4xrmg8kj1#

可以在子查询中聚合表:

WITH j (test) AS (
  SELECT 'abc'
  FROM   dual
)
SELECT XMLSERIALIZE(
         DOCUMENT
         XMLELEMENT(
           "WhseSnapshot",
           XMLELEMENT(
             "mdDC",
             MD_EXTRACT_ID_SEQ.NEXTVAL
           ),
           ( SELECT XMLAGG(
                      XMLELEMENT(
                        "line",
                        XMLFOREST(
                          j.test AS "test"
                        )
                      )
                    )
             FROM   j
           )
         )
       ) AS xml
FROM   DUAL;

其输出:
| XML|
| - -----|
| 1abc|
fiddle

相关问题