2012-09-12 23 views
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%' 

當我使用此查詢將節點添加到現有的列。每次運行它都會添加一列。XML DML(Xpath)查詢禁止重複列。它應該測試插入之前是否存在列

我想要實現的是它應該檢查某個特定節點是否存在映射名稱,以及是否存在不添加此節點。否則添加它。那我下面

Link是http://msdn.microsoft.com/en-us/library/ms175466

回答

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

謝謝。它效果很好 – StackOverflowVeryHelpful

相關問題