以递增顺序在多个节点上插入属性

问题描述 投票:1回答:1

我在SQL Server表中有一个XML列。我想向XML中存在的节点添加一个属性。该属性将是节点的唯一标识符。

我写了一些类似的代码:

DECLARE @i int, @key varchar(20);
SET @key = 'key'
SET @i = 1

IF (@i < 10)
    SET @key = @key + @i

UPDATE copy
SET dataxml.modify('insert attribute key {@key}
                    into (/Package[1]/Section/*[local-name() = ("List", "InputNumber", "InputText")][@i])[1]') 
WHERE id = 10; 

我不确定如何遍历此XML,以及如何准确地将属性添加到正确的节点,因为它具有随机排列的诸如List,InputNumber,InputText之类的节点。

XML看起来像:

<Package>
   <Section name='BOX'>
       <InputNumber description="[1] What is the height of the Box."> 
           <value/> 
       </InputNumber>
       <InputNumber description="[2] What is the width of the Box."> 
           <value/> 
       </InputNumber>
       <List description="[3] What is the type of BOX."> 
           <option/>
      </List>
   </Section>
   <Section name='Cover'>
       <InputText description="[4] Color of cover."> 
          <value/> 
       </InputText>
       <List description="[5] Type of cover."> 
          <option/>
      </List>
    </Section>
</Package>

我希望输出像:

<Package>
   <Section name='BOX'>
       <InputNumber key="key1" description="[1] What is the height of the Box."> 
           <value/> 
       </InputNumber>
       <InputNumber key="key2" description="[2] What is the width of the Box."> 
           <value/> 
       </InputNumber>
       <List key="key3" description="[3] What is the type of BOX."> 
           <option/>
      </List>
   </Section>
   <Section name='Cover'>
       <InputText key="key4" description="[4] Color of cover."> 
          <value/> 
       </InputText>
       <List key="key5" description="[5] Type of cover."> 
          <option/>
      </List>
    </Section>
</Package>

运行上述查询时出现错误:

XQuery [copy.DataXML.modify()]:不支持顶级属性节点

sql-server xml xquery-sql
1个回答
0
投票

您可以在while循环中完成,只要有什么要更新。

declare @T table(dataxml xml not null);

insert into @T(dataxml) values
('<Package>
   <Section name=''BOX''>
       <InputNumber description="[1] What is the height of the Box."> 
           <value/> 
       </InputNumber>
       <InputNumber description="[2] What is the width of the Box."> 
           <value/> 
       </InputNumber>
       <List description="[3] What is the type of BOX."> 
           <option/>
      </List>
   </Section>
   <Section name=''Cover''>
       <InputText description="[4] Color of cover."> 
          <value/> 
       </InputText>
       <List description="[5] Type of cover."> 
          <option/>
      </List>
    </Section>
</Package>');

declare @key int = 1;

while 1 = 1
begin
  update @T
  set dataxml.modify('insert attribute key {sql:variable("@key")} into 
                      (/Package/Section/*[local-name() = ("List", "InputText", "InputNumber") and not(@key)])[1]')
  where dataxml.exist('/Package/Section/*[local-name() = ("List", "InputText", "InputNumber") and not(@key)]') = 1;

  if @@rowcount = 0
    break;

  set @key += 1;

end;

结果:

<Package>
  <Section name="BOX">
    <InputNumber description="[1] What is the height of the Box." key="1">
      <value />
    </InputNumber>
    <InputNumber description="[2] What is the width of the Box." key="2">
      <value />
    </InputNumber>
    <List description="[3] What is the type of BOX." key="3">
      <option />
    </List>
  </Section>
  <Section name="Cover">
    <InputText description="[4] Color of cover." key="4">
      <value />
    </InputText>
    <List description="[5] Type of cover." key="5">
      <option />
    </List>
  </Section>
</Package>
© www.soinside.com 2019 - 2024. All rights reserved.