Sql server T-SQL:通过搜索同级(对等)更新特定的XML节点
我有如下XML,表示SQL表数据:Sql server T-SQL:通过搜索同级(对等)更新特定的XML节点,sql-server,xml,Sql Server,Xml,我有如下XML,表示SQL表数据: <Project> <DataSource version="4" type="LiveDatabaseSource"> <ServerName>(local)</ServerName> </DataSource> <Tables> <value> <TableType>Generate
<Project>
<DataSource version="4" type="LiveDatabaseSource">
<ServerName>(local)</ServerName>
</DataSource>
<Tables>
<value>
<TableType>Generated</TableType>
<PopulationDetails version="2" type="PopulationDetails">
<PopulationType>RowCount</PopulationType>
<RowCount>10000</RowCount>
<ProportionTableExists>False</ProportionTableExists>
<Proportion>0</Proportion>
<TimeToPopulate>0</TimeToPopulate>
</PopulationDetails>
<Name>Table1</Name>
<Schema>dbo</Schema>
</value>
<value>
<TableType>Generated</TableType>
<PopulationDetails version="2" type="PopulationDetails">
<PopulationType>RowCount</PopulationType>
<RowCount>10000</RowCount>
<ProportionTableExists>False</ProportionTableExists>
<Proportion>0</Proportion>
<TimeToPopulate>0</TimeToPopulate>
</PopulationDetails>
<InvalidRowBehaviour>SkipRow</InvalidRowBehaviour>
<Included>False</Included>
<Append>False</Append>
<Name>Table2</Name>
<Schema>dbo</Schema>
</value>
</Tables>
</Project>
有很多基于属性查找节点和更新值的示例,但不是这种类型的对等级搜索或我的google foo糟糕透顶。以下是您要查找的内容 SQL
这可能也有帮助:完美!我不理解它,因为在我看来,它仍然被视为处于等级制度之下,但它是有效的!值[Name=Table2]谓词在XQuery中的作用类似于SQL中的WHERE子句。
UPDATE dbo.SQLGenXML
SET GenXML.modify('replace value of
(/Project/Tables/value/PopulationDetails/RowCount/text()) [1] with ("60000")')
WHERE id = 1;
-- DDL and sample data population, start
DECLARE @SQLGenXML TABLE (ID INT IDENTITY PRIMARY KEY, GenXML XML);
INSERT INTO @SQLGenXML (GenXML)
VALUES
(N'<Project>
<DataSource version="4" type="LiveDatabaseSource">
<ServerName>(local)</ServerName>
</DataSource>
<Tables>
<value>
<TableType>Generated</TableType>
<PopulationDetails version="2" type="PopulationDetails">
<PopulationType>RowCount</PopulationType>
<RowCount>10000</RowCount>
<ProportionTableExists>False</ProportionTableExists>
<Proportion>0</Proportion>
<TimeToPopulate>0</TimeToPopulate>
</PopulationDetails>
<Name>Table1</Name>
<Schema>dbo</Schema>
</value>
<value>
<TableType>Generated</TableType>
<PopulationDetails version="2" type="PopulationDetails">
<PopulationType>RowCount</PopulationType>
<RowCount>10000</RowCount>
<ProportionTableExists>False</ProportionTableExists>
<Proportion>0</Proportion>
<TimeToPopulate>0</TimeToPopulate>
</PopulationDetails>
<InvalidRowBehaviour>SkipRow</InvalidRowBehaviour>
<Included>False</Included>
<Append>False</Append>
<Name>Table2</Name>
<Schema>dbo</Schema>
</value>
</Tables>
</Project>');
-- DDL and sample data population, end
UPDATE @SQLGenXML
SET GenXML.modify('replace value of
(/Project/Tables/value[Name="Table2"]/PopulationDetails/RowCount/text())[1] with ("60000")');
-- test
SELECT * FROM @SQLGenXML;