0

来自德国的早安。

现在,如果 value_expr 为空,有没有办法抑制 XMLElement 中的整个标记?

喜欢:禁止“宾果”标签:

SELECT XMLELEMENT( "artno", trim(artno) )
     , XMLELEMENT( "bingo", NULL )
  FROM articlemasterdata
 WHERE artno = '770100C'

<artnr>770100C</artnr>
<bingo></bingo>

甚至 XMLTYPE 也提供了一个空标签:

SELECT XMLTYPE( XMLELEMENT( "artno", trim(artno) ).getclobval() ) 
    ,  XMLTYPE( XMLELEMENT( "bingo", null ).getclobval() )
  FROM articlemasterdata 
 WHERE artno = '770100C';

<artnr>770100C</artnr>
<bingo></bingo>

谢谢,

约阿希姆

4

1 回答 1

2

您可以使用xmlforest而不是xmlelement

-- CTE for dummy table
with articlemasterdata (artno) as (select '770100C' from dual)
-- actual query
SELECT XMLELEMENT( "artno", trim(artno) )
     , XMLFOREST(NULL as "bingo")
  FROM articlemasterdata
 WHERE artno = '770100C';

XMLELEMENT("ARTNO",TRIM(ARTNO))                                                  XMLFOREST(NULLAS"BINGO")                                                        
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
<artno>770100C</artno>                                                                                                                                           

或者更实际一点:

-- CTE for dummy table
with articlemasterdata (artno, bingo) as (
  select '770100A', 'legs eleven' from dual
  union all
  select '770100C', null from dual
)
-- actual query
SELECT XMLELEMENT( "artno", trim(artno) )
     , XMLFOREST( bingo as "bingo")
  FROM articlemasterdata
 WHERE artno = '770100A'
    OR artno = '770100C';

XMLELEMENT("ARTNO",TRIM(ARTNO))                                                  XMLFOREST(BINGOAS"BINGO")                                                       
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
<artno>770100A</artno>                                                           <bingo>legs eleven</bingo>                                                      
<artno>770100C</artno>                                                                                                                                           

甚至:

SELECT XMLELEMENT( "root",
         XMLELEMENT( "artno", trim(artno) )
       , XMLFOREST( bingo as "bingo")
       )
  FROM articlemasterdata
 WHERE artno = '770100A'
    OR artno = '770100C';

XMLELEMENT("ROOT",XMLELEMENT("ARTNO",TRIM(ARTNO)),XMLFOREST(BINGOAS"BINGO"))    
--------------------------------------------------------------------------------
<root><artno>770100A</artno><bingo>legs eleven</bingo></root>
<root><artno>770100C</artno></root>
于 2019-09-23T09:15:06.350 回答