简体   繁体   English

T-SQL:通过搜索同级(对等)更新特定的 XML 节点

[英]T-SQL: update a specific XML node by searching for a sibling (peer)

I have XML like the following, representing SQL table data:我有 XML 如下,代表 SQL 表数据:

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

This is in a single XML column in SQL Server, and there are many more <value> entries representing tables in the database.这是在 SQL 服务器的单个 XML 列中,并且还有更多<value>条目表示数据库中的表。 I need to update PopulationDetails.RowCount by searching for the value in Name : ie I want to update Table2 to a row count of 60,000 using T-SQL.我需要通过在Name中搜索值来更新PopulationDetails.RowCount :即我想使用 T-SQL 将Table2更新为 60,000 的行数。

I can find the correct node via Name , but I'm having trouble updating the value in its peer PopulationDetails.Name node.我可以通过Name找到正确的节点,但在更新其对等PopulationDetails.Name节点中的值时遇到问题。 All I have so far is the ability to update the nth record:到目前为止,我所拥有的只是更新第 n 条记录的能力:

UPDATE dbo.SQLGenXML
SET GenXML.modify('replace value of 
(/Project/Tables/value/PopulationDetails/RowCount/text())    [1] with ("60000")')
WHERE id = 1;

There are a lot of examples out there for finding nodes based on attributes and updating the value, but not this type of peer level search (or my google-foo sucks).有很多基于属性查找节点和更新值的示例,但不是这种类型的对等级搜索(或者我的 google-foo 很烂)。

Here is what you are looking for.这就是你要找的东西。

SQL SQL

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM