Given a element, defined within an XML Schema Collection as such:
<xsd:element name="xid">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="32" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
How would you update the element using XQuery?
The element is found within the ns namespace in the schema collection. I have been trying to update the element the below query:
update cm.item
set data.modify(
'declare namespace ns="http://www.anon.com";
replace value of (/ns:*/ns:xid)[1] with "X00011793" cast as element(ns{http://www.anon.com}:xid,#anonymous) ?')
where id = 11793
but this generates the following error:
Msg 9301, Level 16, State 1, Line 2 XQuery [cm.item.data.modify()]: In this version of the server, 'cast as ' is not available. Please use the 'cast as ?' syntax.
If I remove the cast entirely and use this query:
update cm.item
set data.modify(
'declare namespace ns="http://www.anon.com";
replace value of (/ns:*/ns:xid)[1] with "X00011793"')
where id = 11793
I get this error:
Msg 2247, Level 16, State 1, Line 2 XQuery [cm.item.data.modify()]: The value is of type "xs:string", which is not a subtype of the expected type "<anonymous>".
If I issue this query:
update cm.item
set data.modify(
'declare namespace ns="http://www.anon.com/";
replace value of (/ns:*/ns:xid/text())[1] with "X00011793"')
where id = 11793
I get this error:
Msg 9312, Level 16, State 1, Line 2 XQuery [cm.item.data.modify()]: 'text()' is not supported on simple typed or 'http://www.w3.org/2001/XMLSchema#anyType' elements, found '(element(ns{http://www.anon.com/}:xid,#anonymous) ?) *'.
I am targeting SQL Server 2008 R2.
Thanks!