3
update serializedvalue set value.modify(' 
insert         
    <GridDataVisibleColumn>
      <FilterBehavior>StronglyTyped</FilterBehavior>
      <FilterBarMode>Immediate</FilterBarMode>
      <AllowFilter>false</AllowFilter>
      <AllowSort>true</AllowSort>
      <AllowDrag>true</AllowDrag>
      <AllowGroup>true</AllowGroup>
      <AllowResize>true</AllowResize>
      <ShowColumnOptions>false</ShowColumnOptions>
      <HeaderText>DeskLabel</HeaderText>
      <IncrementSeed>1</IncrementSeed>
      <IsIdentity>false</IsIdentity>
      <IsReadOnly>true</IsReadOnly>
      <MappingName>Deskl</MappingName>
      <MinimumWidth>0</MinimumWidth>
      <Width>
        <UnitType>Auto</UnitType>
        <Value>1</Value>
      </Width>
      <DataType>String</DataType>
      <UpdateMode>LostFocus</UpdateMode>
      <IsHidden>false</IsHidden>
    </GridDataVisibleColumn>
    as last into (/GridDataTableProperties/VisibleColumns)[1] 

')
where   Token like '%gridsettings%'

当我使用此查询将节点添加到现有列时。每次运行它都会添加一列。

我想要实现的是它应该检查是否存在具有映射名称的特定节点以及是否存在不添加该节点。否则添加它。

我关注的链接是http://msdn.microsoft.com/en-us/library/ms175466

4

1 回答 1

2

将您的 XML 放入一个变量中,提取映射名称并在 where 子句中使用它。

declare @XML xml
set @XML = '
    <GridDataVisibleColumn>
      <FilterBehavior>StronglyTyped</FilterBehavior>
      <FilterBarMode>Immediate</FilterBarMode>
      <AllowFilter>false</AllowFilter>
      <AllowSort>true</AllowSort>
      <AllowDrag>true</AllowDrag>
      <AllowGroup>true</AllowGroup>
      <AllowResize>true</AllowResize>
      <ShowColumnOptions>false</ShowColumnOptions>
      <HeaderText>DeskLabel</HeaderText>
      <IncrementSeed>1</IncrementSeed>
      <IsIdentity>false</IsIdentity>
      <IsReadOnly>true</IsReadOnly>
      <MappingName>Deskl</MappingName>
      <MinimumWidth>0</MinimumWidth>
      <Width>
        <UnitType>Auto</UnitType>
        <Value>1</Value>
      </Width>
      <DataType>String</DataType>
      <UpdateMode>LostFocus</UpdateMode>
      <IsHidden>false</IsHidden>
    </GridDataVisibleColumn>'

declare @MappingName varchar(50)
select @MappingName = @XML.value('(/GridDataVisibleColumn/MappingName/text())[1]', 'varchar(50)')

update serializedvalue set value.modify(' 
  insert         
    sql:variable("@XML")
    as last into (/GridDataTableProperties/VisibleColumns)[1] 
')
where Token like '%gridsettings%' and
      value.exist('/GridDataTableProperties
                    /VisibleColumns
                      /GridDataVisibleColumn
                        [MappingName = sql:variable("@MappingName")]') = 0
于 2012-09-12T15:44:24.550 回答