August 7, 2018 at 11:35 pm
create xml schema collection xmlschema1 asgo
Declare @x0 XML = '<root id="33"/>'
Declare @x1 XML (xmlschema1) = '<root id="33"/>'
Declare @x2 XML (xmlschema2) = '<root id="33"/>'
Select X.N.value('@id', 'int') As v0 From @x0.nodes('root') X(N)
--Select X.N.value('@id', 'int') As v1 From @x1.nodes('root') X(N) -- This fails because the attribute isn't a singleton?
-- Error message is:
-- XQuery [value()]: 'value()' requires a singleton (or empty sequence), found operand of type 'xdt:anyAtomicType *'
Select X.N.value('@id', 'int') As v2 From @x2.nodes('root') X(N)
go
drop xml schema collection xmlschema1
drop xml schema collection xmlschema2
August 8, 2018 at 1:04 am
There are some possible factors which could cause this, one is that the XML only has a top level element, which can cause problems. The second thing is that although the breaking XML is typed, the attribute is still untyped and that changes the way the value is retrieved. Adding any applicable datatype definition will fix the problem.
August 8, 2018 at 6:30 am
Select X.N.value('string(@id)', 'int') As v1 From @x1.nodes('root') X(N)
Here is a link to a similar case with an explanation on why by the designer of the XML datatype in SQL Server.
Found operand of type 'xdt:anyAtomicType *'. Why?!?!
August 8, 2018 at 6:53 am
Interesting, you learn something every day. Thanks chaps.
Viewing 4 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy