使用modify T-SQL 2008从xml数据类型中删除行

时间:2014-07-10 13:04:42

标签: xml sql-server-2008

我试图从xml数据类型字段中删除一些行。我已经阅读了很多指南但很遗憾看起来我需要一点推动。如果有人可以帮助我,我们将不胜感激。这是我的代码:

DECLARE @Xml TABLE (Data XML)
DECLARE @Data XML

set @Data=N'
<resultset>
    <row>
        <column name="PieceNo">1</column>
        <column name="PieceNoEvalUnit">655360</column>
        <column name="PieceIdent">Function check1</column>
        <column name="RequestNo">1</column>
    </row>
    <row>
        <column name="PieceNo">2</column>
        <column name="PieceNoEvalUnit">655362</column>
        <column name="PieceIdent">Function check2</column>
        <column name="RequestNo">1</column>
    </row>
    <row>
        <column name="PieceNo">3</column>
        <column name="PieceNoEvalUnit">655364</column>
        <column name="PieceIdent">Function check3</column>
        <column name="RequestNo">1</column>
    </row>
</resultset>'

INSERT  INTO @Xml( Data)
values(@data)

--update @xml set data.modify('
--delete (/resultset/row)[/column/text()[1]>="2"]
--')

--update @xml set data.modify('
--delete //row//column[text()][.PieceNo >= sql:variable("2")]
--')

select 
X.N.value('(column/text())[1]', 'int') as PieceNo,
X.N.value('(column/text())[2]', 'int') as PieceNoEvalUnit,
X.N.value('(column/text())[3]', 'varchar(255)') as PieceIdent,
X.N.value('(column/text())[4]', 'int') as RequestNo
from 
@Xml as T
cross apply t.Data.nodes(N'/resultset/row') as X(N)

让我们举例说,当PieceNo&gt; = 2时,我想从xml中删除一些行。

谢谢

1 个答案:

答案 0 :(得分:0)

您可以使用以下命令:

update @xml set data.modify('
    delete /resultset/row[column[@name="PieceNo"][.>=2]]
')

高于XPath意味着选择具有子节点<row>的{​​{1}} {}} {}} {}}} {}}