[英]Convert VARCHAR column containing XML to table
I am working with VMware vCenter 5 and want to create a quick report of the DRS rules that have been created. 我正在使用VMware vCenter 5,并希望创建已创建的DRS规则的快速报告。 Below is an excerpt of the data:
以下是数据的摘录:
<obj xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:vim25" versionId="5.0" xsi:type="ArrayOfClusterRuleInfo">
<ClusterRuleInfo xsi:type="ClusterAffinityRuleSpec">
<key>1</key>
<enabled>true</enabled>
<name>SQL Servers</name>
<vm type="VirtualMachine" xsi:type="ManagedObjectReference">vm-100</vm>
<vm type="VirtualMachine" xsi:type="ManagedObjectReference">vm-200</vm>
<vm type="VirtualMachine" xsi:type="ManagedObjectReference">vm-300</vm>
</ClusterRuleInfo>
<ClusterRuleInfo xsi:type="ClusterAntiAffinityRuleSpec">
<key>2</key>
<enabled>true</enabled>
<name>Oracle DBs</name>
<vm type="VirtualMachine" xsi:type="ManagedObjectReference">vm-1000</vm>
<vm type="VirtualMachine" xsi:type="ManagedObjectReference">vm-2000</vm>
</ClusterRuleInfo>
</obj>
Note that the number of VMs per rule may differ. 请注意,每个规则的VM数可能不同。
I have not been able to turn this XML data into a table, so I tried locating just the name (within the tags) with this query: 我无法将此XML数据转换为表格,因此我尝试使用此查询仅查找名称(在标记内):
SELECT ref.value('name[1]', 'varchar(200)') as [enabled]
FROM @data.nodes('/obj/ClusterRuleInfo') data( ref )
It returns no data. 它不返回任何数据。 I found that if I remove the and lines, it works, but I'm not sure why.
我发现,如果我删除和线条,它可以工作,但我不知道为什么。 I believe it is because SQL Server does not understand the XML schema (or something similar - I'm not an XML expert).
我相信这是因为SQL Server不理解XML模式(或类似的东西 - 我不是XML专家)。
In the end, I'd like to see a table like this: 最后,我想看到这样一个表格:
Name VM
----------- ----
SQL Servers 100
SQL Servers 200
SQL Servers 300
Oracle DBs 1000
Oracle DBs 2000
Your XML uses a namespace, so you need the WITH XMLNAMESPACES
clause: 您的XML使用命名空间,因此您需要
WITH XMLNAMESPACES
子句:
declare @data xml;
set @data = '
<obj xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:vim25" versionId="5.0" xsi:type="ArrayOfClusterRuleInfo">
<ClusterRuleInfo xsi:type="ClusterAffinityRuleSpec">
<key>1</key>
<enabled>true</enabled>
<name>SQL Servers</name>
<vm type="VirtualMachine" xsi:type="ManagedObjectReference">vm-100</vm>
<vm type="VirtualMachine" xsi:type="ManagedObjectReference">vm-200</vm>
<vm type="VirtualMachine" xsi:type="ManagedObjectReference">vm-300</vm>
</ClusterRuleInfo>
<ClusterRuleInfo xsi:type="ClusterAntiAffinityRuleSpec">
<key>2</key>
<enabled>true</enabled>
<name>Oracle DBs</name>
<vm type="VirtualMachine" xsi:type="ManagedObjectReference">vm-1000</vm>
<vm type="VirtualMachine" xsi:type="ManagedObjectReference">vm-2000</vm>
</ClusterRuleInfo>
</obj>';
WITH XMLNAMESPACES('urn:vim25' AS ns)
SELECT
ref.value('../ns:name[1]','nvarchar(256)') AS Name,
STUFF(ref.value('.','nvarchar(256)'),1,3,'') AS VM
FROM @data.nodes('/ns:obj/ns:ClusterRuleInfo/ns:vm') data( ref );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.