할 수 있는 방법을 동적으로 변경 XML 은 구조의 문자열에서 SQL

0

질문

내가 필요로 하는 SQL 스크립트를 당겨하는 XML 문자열에서 DB[varchar(max)],검사,업데이트 하는 경우 에 맞는 특정 상황이다.

상상하는 xml 형식은 다음과 같습니다:

<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>

그래서 무엇을 원하는 것입 업데이트 모든 요소 누구의 이름은"level6"고 있는 특성이라고"여기에서"그 값을 시작 으로"지금 이 시간입니다". 그래서,그와 일치해야만 두 가지 요소이다.

하지만,그만 선정기준입니다. 목록 옵션 포함되지 않아야 합니다 <option here="now" />. 그래서,그 두어야 우리 하나의 요소를 업데이트합니다.

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

하는 요소에,나는 다음을 추가 누락 <option here="now" />도록된:

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

그래서,최종 결과에 이어야 한다:

 <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>

가정 내가 읽을 수 있는 데이터의 DB 문자열로,그리고 내가 알고있는 어떻게 업데이트 DB,그래서 그것은 정말 어떻게 조정하는 xml 문자열에서 SQL(SQL Server).

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

최고의 응답

1

당신이 사용할 수 있는 XML DML(data modification)을 .modify 을 변경하는 기능을 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]');

다음과 같이 작동합니다:

  • insert <option here="now" /> 이 값입니다 우리는 삽입
  • as last into 그것은 다른 아이의 노드를 선택한 하나
  • /root/level1/level2/level3/level4/level5/level6 이것을 우리는 level6 노드
  • [substring(@here, 1, 15) = "now is the time"] 조건자는 노드가 here 특성으로 시작하는 값입니다. 수정해야 합니다.length 매개 변수 값과 일치하는 비교. 이 없 LIKE 에 XQuery
  • /options [not(/option[@here = "now"])] 우리는 options 노드가 없음 option 아동서 here="now" 특성
  • [1] 첫 번째 같은 노드

을 수정해야 하는 경우 여러 개의 노드에서 하나의 XML 문서를 실행해야 하는 이 루프

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;

이 작업을 수행할 수도 있습에 대한 전체를 테이블

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;

대용량 데이터 그것은 빠르게 할 수 있습을 다시 전체를 사용하여 XML XQuery,여분의 노드를 사용하여 추가로 건설 XML.

db<>바이올린

2021-11-23 23:41:04

다른 언어로

이 페이지는 다른 언어로되어 있습니다

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