문제

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.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)') 
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top