Query to output content of AllXml column of ELMAH_Error sql server table

Go To StackoverFlow.com

2

I am having trouble writing query so that I can query the content of AllXml column inside Elmah_Error table.

How can I list out all the item nodes as an output of the query. How could I write query to only list for certain item nodes?

I would like to get follow resultset:

item value

===== =====

ALL_HTTP HTTP_CONNECTION:xxxx

ALL_RAW Connection: xxxxx

I would also like to be able to filter the query by ErrorID

Content of AllXml column may look like this.

<error
  application="/"
  message="hello world"
  source="TestWebElmah"
  detail="xxxxx">
  <serverVariables>
    <item
      name="ALL_HTTP">
      <value
        string="HTTP_CONNECTION:xxxx" />
    </item>
    <item
      name="ALL_RAW">
      <value
        string="Connection: xxxxx" />
    </item>

  </serverVariables>
</error>
2012-04-04 21:29
by dotnet-practitioner


7

Remote Addr nodes

select T.N.value('(value/@string)[1]', 'varchar(30)') as REMOTE_ADDR
from 
(select cast(AllXml as xml) as AllXml from ELMAH_Error) e
   cross apply AllXml.nodes('//item[@name="REMOTE_ADDR"]') as T(N)

HTTP User Agents which contain mozilla

select T.N.value('(value/@string)[1]', 'varchar(30)') as HTTP_USER_AGENT
from 
(select cast(AllXml as xml) as AllXml from ELMAH_Error) e
   cross apply AllXml.nodes('//item[@name="HTTP_USER_AGENT"]') as T(N)
where T.N.value('(value/@string)[1]', 'varchar(30)') like '%mozilla%'

Elmah table stores the AllXml column as nvarchar so it needs to be casted to xml

all tags + values, by error id

select T.N.value('@name', 'varchar(30)') as Name,
       T.N.value('(value/@string)[1]', 'varchar(30)') as Value
from 
(select cast(AllXml as xml) as AllXml from ELMAH_Error where ErrorId = 'DC82172B-F2C0-48CE-8621-A60B702ECF93') e
cross apply AllXml.nodes('/error/serverVariables/item') as T(N)




Before voting down this answer, because uses most of the part of Mikael Eriksson's answer, I let you know I'll happily accept the downvotes only for this reason, since is mainly true

2012-04-04 22:45
by Adrian Iftode
this works great.. thanks. - dotnet-practitioner 2012-04-04 22:49
How can I get list of all the item and values in AllXml column - dotnet-practitioner 2012-04-04 22:49
see my update. I removed the top 10 clause also, because I have a huge elmah table and takes a while to test it. - Adrian Iftode 2012-04-04 22:58
thanks Adrian... How could I filter by specific error in Elmah table.. ie ErrorID?.. - dotnet-practitioner 2012-04-04 22:58
welcome, by Id is easy, even is a GUID, you can place the guid like any other strin - Adrian Iftode 2012-04-04 23:02
I would like to filter the 'all tags + values' result by ErrorID.. Please let me know.. and thank you for all your hel - dotnet-practitioner 2012-04-04 23:03
welcome, update - Adrian Iftode 2012-04-04 23:07
Thanks Adrian... thank you very much... - dotnet-practitioner 2012-04-04 23:11
+1 from me. A good nights sleep got in the way of updating my answer - Mikael Eriksson 2012-04-05 02:58


1

This query will give you all item nodes

select T.N.value('@name', 'varchar(30)') as Name,
       T.N.value('(value/@string)[1]', 'varchar(30)') as Value
from Elmah_Error
  cross apply AllXml.nodes('/error/serverVariables/item') as T(N)

If you want to filter on any of the values you can put that in a sub-query apply a regular where clause.

select Name, 
       Value
from
  (
    select T.N.value('@name', 'varchar(30)') as Name,
           T.N.value('(value/@string)[1]', 'varchar(30)') as Value
    from Elmah_Error
      cross apply AllXml.nodes('/error/serverVariables/item') as T(N)
  ) T
where Name = 'ALL_HTTP'
2012-04-04 22:18
by Mikael Eriksson
the only problem with Elmah table is that the AllXml column is not of xml type, so it needs a cas - Adrian Iftode 2012-04-04 22:23
Mikael, when I run your queries, I get the following error... The XMLDT method 'nodes' can only be invoked on columns of type xml. I noticed that AllXml column is of type ntext. Please assist me.. thank - dotnet-practitioner 2012-04-04 22:26
@dotnet-practitioner, you need to cast it to xml probably Mikael Eriksson will show you ho - Adrian Iftode 2012-04-04 22:27
I am trying but no luck so far - dotnet-practitioner 2012-04-04 22:30
@dotnet-practitioner - Timezone issues and a good nights sleep got in the way of updating the answer with the cast from varchar. But I see that you have it resolved now - Mikael Eriksson 2012-04-05 02:55
Ads