Query ELMAH's XML field
The stock ELMAH_Error table uses an nText field to store an Error entry. I found that by adding a field of type XML; then adding this new field to the INSERT statement of the SPROC that populates the field; i could make better use of ELMAH's output.
Now I'd like to learn how to query specific element values within that XML field. The document is structured as:
<error [...]>
<serverVariables>
<item name="ALL_HTTP">
<value string="..." />
</item>
<item name="ALL_RAW">
<value string="..." />
</item>
.
.
.
</serverVariables>
</error>
I need to be able to query the value of specific items beneath .
So I'm looking at an example from the 15seconds.com article:
SELECT MyXml.value('(/root/product[@id="304"]/name)[1]', 'nvarchar(30)')
and am trying to maps those values to my field's structure - but can't. E.g.
select top 10 RealXML.value('(/error/serverVariables[@id="REMOTE_HOST"]/name)[0]', 'nvarchar(30)')
where REMOTE_HOST is formatted:
<item name="REMOTE_HOST">
<value string="55开发者_如何学JAVA.55.55.55" />
</item>
much appreciated
This should work:
select top 10 RealXML.value('(/error/serverVariables/item[@name="REMOTE_HOST"]/value/@string)[1]', 'nvarchar(30)')
Tested using the following:
DECLARE @xml XML = '
<error>
<serverVariables>
<item name="ALL_HTTP">
<value string="..." />
</item>
<item name="ALL_RAW">
<value string="..." />
</item>
<item name="REMOTE_HOST">
<value string="55.55.55.55" />
</item>
</serverVariables>
</error>
'
SELECT @xml.value('(/error/serverVariables/item[@name="REMOTE_HOST"]/value/@string)[1]','nvarchar(30)')
精彩评论