我在编写查询时遇到问题,这样我就可以在Elmah_Error表中查询AllXml列的内容。
如何列出所有项目节点作为查询的输出。 我怎样才能将查询编写为仅列出某些项目节点?
我想得到以下结果集:
项目值
===== =====
ALL_HTTP HTTP_CONNECTION:xxxx
ALL_RAW连接:xxxxx
我还希望能够通过ErrorID过滤查询
AllXml列的内容可能如下所示。
<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>
答案 0 :(得分:7)
远程地址节点
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)
包含 mozilla
的HTTP用户代理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表将AllXml列存储为nvarchar,因此需要将其转换为xml
所有标记+值,错误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)
在对这个答案进行投票之前,因为使用了Mikael Eriksson答案的大部分内容,我告诉你我只会因为这个原因而高兴地接受downvotes,因为主要是真的
答案 1 :(得分:1)
此查询将为您提供所有item
个节点
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)
如果要过滤任何可以放在子查询中的值,请应用常规的where子句。
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'