我在 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>
如何选择“类型”和“键”,以便从该列的 XML 数据中获取单行的表格结果?该表有一个名为 HistoryId 的标识主键。
最佳答案
;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)
关于sql-server - 从 varchar(max) 列中选择 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4429568/