May 4, 2009 at 6:40 am
Hi, i can update the content for specific position.
by replacing the line 10
[
SET @newXML.modify('replace value of (row/rownew/@*[local-name() =sql:variable("@attrName")])[1] with sql:variable("@attrValue")')
] but i want modify by looping..???
1) DECLARE @newXML xml;
2) declare @count int;
3)DECLARE @attrName nvarchar(100), @attrValue nvarchar(200);
4)set @attrValue='B'
5)SET @newXML = '
6)SET @attrName = 'text';
7)set @count=1
8)WHILE(@count <= 2)
9)BEGIN
10)SET @newXML.modify('replace value of (row/rownew/@*[local-name() =sql:variable("@attrName")])[position()=sql:variable("@count")] with sql:variable("@attrValue")')
11)set @count=@count+1
12)end
13)select @newXML
Any body can tell me how to do this ?
May 4, 2009 at 10:23 am
Hi,
I assume your getting the following error message:
Msg 2337, Level 16, State 1, Line xx
XQuery [modify()]: The target of 'replace' must be at most one node, found 'attribute(*,xdt:untypedAtomic) *'
The reason is you have to reference the sequence number of the attribute within the element you're changing.
Instead of
SET @newXML.modify('replace value of (row/rownew/@*[local-name() =sql:variable("@attrName")])[position()=sql:variable("@count")] with sql:variable("@attrValue")')
you should use
SET @newXML.modify('replace value of (row/rownew/@*[local-name() =sql:variable("@attrName")])[position()=sql:variable("@count")][1] with sql:variable("@attrValue")')
to refer to the first occurence of @attrName.
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply