4

This is my xml stored in an XML type column called UserDef

<Orders>
  <Document>
    <DocumentID>abc123</DocumentID>
    <ActualShipDate />
    <Comments/>
  </Document>
  <Document>
 ....
 ...
  </Document>
</Orders>

I'm trying to populate the Comments element with this:

declare @t1 table (id bigint, userdef xml)

insert into @t1
select id, userdef from MyMainTable

update @t1 
set userdef.modify('replace value of(/Orders/Document/Comments[1]/text())[1]  with "NO COMMENTS"')
select * from @t1   

However, I don't see the Comments being populated at all. What should I do differently in order for this to work?

4

1 回答 1

4

元素<Comments/>没有text()节点,你应该这样insertreplace

    update @t1 
    set userdef.modify('
        insert text{"NO COMMENTS"}
        into (/Orders/Document/Comments[1])[1]
    ')
    where id = @id;

如果希望从 sql 变量中插入文本,可以使用以下构造:

declare @comments varchar(1000);
set @comments = 'NO COMMENTS';

update @t1 
set userdef.modify('
    insert text {sql:variable("@comments")}
    into (/Orders/Document/Comments[1])[1]
')
where id = @id;
于 2013-07-30T20:22:57.807 回答