Ako môžem dynamicky meniť XML štruktúra reťazca v SQL

0

Otázka

Potrebujem SQL skript, ktorý bude ťahať XML reťazec z DB [varchar(max)], kontrolu a aktualizáciu to , ak sa to hodí konkrétnej situácii.

Predstavte si, že moje xml je v nasledovnom formáte:

<root>
  <level1>
    <level2>
      <level3 />
      <level3 />
    </level2>
  </level1>
  <level1>
    <level2>
      <level3>
        <level4>
          <level5>
            <level6 here="now is the time for XYZ">
              <options>
                <option this="that" />
                <option me="you" />
              </options>
            </level6>
          </level5>
        </level4>
      </level3>
    </level2>
  </level1>
  <level1>
    <level2>
      <level3>
        <level4>
          <level5>
            <level6 here="this one is not of interest">
              <options>
                <option this="that" />
                <option me="you" />
              </options>
            </level6>
          </level5>
        </level4>
      </level3>
    </level2>
  </level1>
  <level1>
    <level2>
      <level3>
        <level4>
          <level5>
            <level6 here="now is the time for ABC">
              <options>
                <option this="that" />
                <option me="you" />
                <option here="now" />
              </options>
            </level6>
          </level5>
        </level4>
      </level3>
    </level2>
  </level1>
</root>

Takže, čo chcem urobiť, je aktualizovať všetky prvky, ktorého názov je "level6" a ktoré majú atribút s názvom "tu", ktorého hodnota sa začína s "teraz je čas". Tak, že by sa zápas len dva prvky vyššie.

Ale, to nie je len výber kritérií. Zoznam možností nesmie obsahovať <option here="now" />. Tak, že by nás nechať len jeden prvok, ktorý chcete aktualizovať.

<level6 here="now is the time for XYZ">
    <options>
        <option this="that" />
        <option me="you" />
    </options>
 </level6>

Na tento prvok, potom som chcete pridať chýbajúce <option here="now" />tak , že sa stáva:

<level6 here="now is the time for XYZ">
    <options>
        <option this="that" />
        <option me="you" />
        <option here="now" />
    </options>
 </level6>

Tak, konečný výsledok by mal byť:

 <root>
  <level1>
    <level2>
      <level3 />
      <level3 />
    </level2>
  </level1>
  <level1>
    <level2>
      <level3>
        <level4>
          <level5>
            <level6 here="now is the time for XYZ">
              <options>
                <option this="that" />
                <option me="you" />
                <option here="now" />      // <- this one new
              </options>
            </level6>
          </level5>
        </level4>
      </level3>
    </level2>
  </level1>
  <level1>
    <level2>
      <level3>
        <level4>
          <level5>
            <level6 here="this one is not of interest">
              <options>
                <option this="that" />
                <option me="you" />
              </options>
            </level6>
          </level5>
        </level4>
      </level3>
    </level2>
  </level1>
  <level1>
    <level2>
      <level3>
        <level4>
          <level5>
            <level6 here="now is the time for ABC">
              <options>
                <option this="that" />
                <option me="you" />
                <option here="now" />
              </options>
            </level6>
          </level5>
        </level4>
      </level3>
    </level2>
  </level1>
</root>

Predpokladajme, že môžem čítať údaje z DB do reťazca, a že viem, ako aktualizovať DB, takže je to naozaj ako na manipuláciu s xml reťazec v SQL (SQL Server).

sql-server tsql xml xquery
2021-11-23 17:17:51
1

Najlepšiu odpoveď

1

Môžete použiť XML DML (data úprave) s .modify funkcia zmeniť XML.

SET @xml.modify('
  insert <option here="now" />
  as last into
  ( /root/level1/level2/level3/level4/level5/level6
     [substring(@here, 1, 15) = "now is the time"]
     /options [not(/option[@here = "now"])]
   )[1]');

Funguje to takto:

  • insert <option here="now" /> toto je hodnota sme vloženie
  • as last into to ide po ďalšie dieťa uzly z vybranej
  • /root/level1/level2/level3/level4/level5/level6 toto sa nám, že level6 uzol
  • [substring(@here, 1, 15) = "now is the time"] predikáty uzol mať here atribút počnúc touto hodnotou. Musíte upraviť dĺžku parameter zhodovať s hodnotou porovnávate. Nie je LIKE v XQuery
  • /options [not(/option[@here = "now"])] sme hľadať options uzol, ktorý má č. option dieťa, ktoré má následne here="now" atribút
  • [1] prvý takýto uzol

Ak musíte zmeniť viacero uzlov v rámci jediného dokumentu XML, budete musieť spustiť v slučky

DECLARE @i int = 20; --max nodes

WHILE @xml.exist('
  /root/level1/level2/level3/level4/level5/level6
     [substring(@here, 1, 15) = "now is the time"]
     /options [not(option[@here = "now"])]
   ') = 1
BEGIN

    SET @xml.modify('
      insert <option here="now" /> as last into
      ( /root/level1/level2/level3/level4/level5/level6
         [substring(@here, 1, 15) = "now is the time"]
         /options [not(option[@here = "now"])]
       )[1]');
     
    SET @i -= 1;
    IF @i = 0
        BREAK;
END;

Môžete tiež urobiť pre celú tabuľku

DECLARE @i int = 20; --max nodes

WHILE EXISTS (SELECT 1
    FROM YourTable
    WHERE XmlColumn.exist('
      /root/level1/level2/level3/level4/level5/level6
         [substring(@here, 1, 15) = "now is the time"]
         /options [not(option[@here = "now"])]
       ') = 1)
BEGIN

    UPDATE t
    SET XmlColumn.modify('
      insert <option here="now" /> as last into
      ( /root/level1/level2/level3/level4/level5/level6
         [substring(@here, 1, 15) = "now is the time"]
         /options [not(option[@here = "now"])]
       )[1]')
    FROM YourTable t
    WHERE XmlColumn.exist('
      /root/level1/level2/level3/level4/level5/level6
         [substring(@here, 1, 15) = "now is the time"]
         /options [not(option[@here = "now"])]
       ') = 1;
     
    SET @i -= 1;
    IF @i = 0
        BREAK;
END;

Pre veľmi veľké súbory dát môže byť rýchlejšie obnoviť celý XML pomocou XQuery, s možnosťou uzol pridané Konštruované pomocou XML.

db - <>husle

2021-11-23 23:41:04

V iných jazykoch

Táto stránka je v iných jazykoch

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................