Oracle:在现有文本之前插入带有 </br> 的文本

Oracle : Insert text with </br> right before existing text

提问人:Rico Strydom 提问时间:11/16/2023 最后编辑:Rico Strydom 更新时间:11/17/2023 访问量:37

问:

我的数据库中有一个 CLOB 列,其中包含如下内容:

<?xml version="1.0"?>
<tdfmt sel-start="218">31 05 2022 Rico Strydom<br/>
Op verzoek (zie e-mail) garanties overplaatsen....<br/>
    <br/>
OVERBOEKING CREDITGELDEN:<br/>BANKCODE: 1171<br/>T.L.V.: VERP. NL99BANK999999999999<br/>T.G.V.: RC NL00BANK000000000000<br/>
BEDRAG: EUR 9999999<br/>BESCHRIJVING: Vrijgave verpanding creditgelden AA98987987987 ivm overzetten zekerheid naar BGF<br/>
    <br/>
</tdfmt>

我想通过在根 (tdfmt) 后面添加文本后再添加另一个文本来更新此字段</br>

导致:

<?xml version="1.0"?>
<tdfmt sel-start="218">THIS TEXT SHOULD GO FIRST</br>
31 05 2022 Rico Strydom<br/>
Op verzoek (zie e-mail) garanties overplaatsen....<br/>
    <br/>
OVERBOEKING CREDITGELDEN:<br/>BANKCODE: 1171<br/>T.L.V.: VERP. NL99BANK999999999999<br/>T.G.V.: RC NL00BANK000000000000<br/>
BEDRAG: EUR 9999999<br/>BESCHRIJVING: Vrijgave verpanding creditgelden AA98987987987 ivm overzetten zekerheid naar BGF<br/>
    <br/>
</tdfmt>

我试过用它来更新我的 clob,但我怀疑 CHR(13) 是要走的路。

UPDATE OIT SET INFTXT = UPDATEXML(XMLTYPE(OIT.INFTXT),'//tdfmt/text()[1]','THIS TEXT SHOULD GO FIRST' || chr(10)  || EXTRACTVALUE(XMLTYPE(OIT.INFTXT), '//tdfmt/text()[1]', '')).getClobVal()

希望我的愿望是明确的......

Oracle PLSQL CLOB

评论


答:

1赞 Alex Poole 11/17/2023 #1

您可以将 XMLQuery 与 FLWOR 一起使用,而不是 UPDATEXML:

UPDATE OIT SET INFTXT = XMLQUERY(q'^
      copy $i := $d modify (
        for $j in $i/tdfmt/text()[1]
          return insert node ( $text, $br, '&#xa;' ) before $j
      )
      return $i
    ^'
    PASSING
      XMLTYPE(OIT.INFTXT) AS "d",
      'THIS TEXT SHOULD GO FIRST' AS "text",
      XMLTYPE('<br/>') AS "br"
    RETURNING CONTENT
  ).getClobVal();

它为您提供:

<?xml version="1.0"?><tdfmt sel-start="218">THIS TEXT SHOULD GO FIRST<br/>
31 05 2022 Rico Strydom<br/>
Op verzoek (zie e-mail) garanties overplaatsen....<br/><br/>
OVERBOEKING CREDITGELDEN:<br/>BANKCODE: 1171<br/>T.L.V.: VERP. NL99BANK999999999999<br/>T.G.V.: RC NL00BANK000000000000<br/>
BEDRAG: EUR 9999999<br/>BESCHRIJVING: Vrijgave verpanding creditgelden AA98987987987 ivm overzetten zekerheid naar BGF<br/><br/></tdfmt>

或者,为了保留(我认为,主要是)您现有的格式,请使用 XMLSerialise 而不是 getClobVal:

UPDATE OIT SET INFTXT = XMLSERIALIZE(DOCUMENT XMLQUERY(q'^
      copy $i := $d modify (
        for $j in $i/tdfmt/text()[1]
          return insert node ( $text, $br, '&#xa;' ) before $j
      )
      return $i
    ^'
    PASSING
      XMLTYPE(OIT.INFTXT) AS "d",
      'THIS TEXT SHOULD GO FIRST' AS "text",
      XMLTYPE('<br/>') AS "br"
    RETURNING CONTENT
  ) AS CLOB INDENT SIZE=4);

它为您提供:

<?xml version="1.0"?>
<tdfmt sel-start="218">THIS TEXT SHOULD GO FIRST<br/>
31 05 2022 Rico Strydom<br/>
Op verzoek (zie e-mail) garanties overplaatsen....<br/>
    <br/>
OVERBOEKING CREDITGELDEN:<br/>BANKCODE: 1171<br/>T.L.V.: VERP. NL99BANK999999999999<br/>T.G.V.: RC NL00BANK000000000000<br/>
BEDRAG: EUR 9999999<br/>BESCHRIJVING: Vrijgave verpanding creditgelden AA98987987987 ivm overzetten zekerheid naar BGF<br/>
    <br/>
</tdfmt>

小提琴

我已经将文本字符串和标签作为参数传递,假设至少文本部分确实是一个变量;如果您愿意,也可以传入换行符:<br/>

UPDATE OIT SET INFTXT = XMLSERIALIZE(DOCUMENT XMLQUERY('
      copy $i := $d modify (
        for $j in $i/tdfmt/text()[1]
          return insert node ( $text, $br, $newline ) before $j
      )
      return $i
    '
    PASSING
      XMLTYPE(OIT.INFTXT) AS "d",
      'THIS TEXT SHOULD GO FIRST' AS "text",
      XMLTYPE('<br/>') AS "br",
      CHR(10) as "newline"
    RETURNING CONTENT
  ) AS CLOB INDENT SIZE=4);

小提琴

...虽然我不确定你是否真的想要/需要它。

评论

0赞 Rico Strydom 11/17/2023
你是我的英雄!!