我在SQL Server 2005上的varchar(max)列中存储了一些XML数据。数据的格式为(FQTN =完全限定类型名称):
<?xml version="1.0" encoding="utf-16"?>
<History xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<EntityViews>
<EntityProxy Type="FQTN" Key="386876" />
<EntityProxy Type="FQTN" Key="387981" />
<!-- etc. -->
</EntityViews>
</History>
如何选择Type,Key以便从此列的XML数据中获取单行的表格结果?该表具有名为HistoryId的标识主键。
答案 0 :(得分:9)
;with cteCastToXML as (
select CAST(YourColumn as xml) as x
from YourTable
)
select h.ep.value('@Type','varchar(10)') as [Type],
h.ep.value('@Key', 'varchar(10)') as [Key]
from cteCastToXML
cross apply x.nodes('/History/EntityViews/EntityProxy') as h(ep)
答案 1 :(得分:0)
我的建议是双重的。