这是T-Sql xml query with namespace
的后续问题我有以下XML数据。我需要将其转换为SQL表。我使用了以下SQL查询:
declare @XML xml =
'<?xml version="1.0" encoding="utf-8"?>
<RS xmlns="urn:schemas-microsoft-com:xml-analysis:rowset">
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:saw-sql="urn:saw-sql" targetNamespace="urn:schemas-microsoft-com:xml-analysis:rowset">
<xsd:complexType name="R">
<xsd:sequence>
<xsd:element name="C0" type="xsd:string" minOccurs="1" maxOccurs="1" saw-sql:type="varchar" saw-sql:sqlFormula=""CBR"."Product Binary"."Binary Name"" saw-sql:displayFormula=""Product Binary"."Binary Name"" saw-sql:aggregationRule="none" saw-sql:aggregationType="nonAgg" saw-sql:tableHeading="Product Binary" saw-sql:columnHeading="Binary Name" saw-sql:columnID="ca5ff07f42dfb3ea1"/>
<xsd:element name="C1" type="xsd:string" minOccurs="1" maxOccurs="1" saw-sql:type="varchar" saw-sql:sqlFormula=""CBR"."Product Binary"."Download Display Name"" saw-sql:displayFormula=""Product Binary"."Download Display Name"" saw-sql:aggregationRule="none" saw-sql:aggregationType="nonAgg" saw-sql:tableHeading="Product Binary" saw-sql:columnHeading="Download Display Name" saw-sql:columnID="c1f02dadb0dfa49a0"/>
<xsd:element name="C2" type="xsd:string" minOccurs="1" maxOccurs="1" saw-sql:type="varchar" saw-sql:sqlFormula=""CBR"."Download Start Date"."Quarter"" saw-sql:displayFormula=""Download Start Date"."Quarter"" saw-sql:aggregationRule="none" saw-sql:aggregationType="nonAgg" saw-sql:tableHeading="Download Start Date" saw-sql:columnHeading="Quarter" saw-sql:columnID="c66bc4e116b33b4ef"/>
<xsd:element name="C3" type="xsd:date" minOccurs="1" maxOccurs="1" saw-sql:type="date" saw-sql:sqlFormula=""CBR"."Download Start Date"."Cal Date"" saw-sql:displayFormula=""Download Start Date"."Cal Date"" saw-sql:aggregationRule="none" saw-sql:aggregationType="nonAgg" saw-sql:tableHeading="Download Start Date" saw-sql:columnHeading="Cal Date" saw-sql:columnID="c5bb40fa14f988e4a"/>
<xsd:element name="C4" type="xsd:string" minOccurs="1" maxOccurs="1" saw-sql:type="varchar" saw-sql:sqlFormula=""CBR"."Download Start Date"."Week"" saw-sql:displayFormula=""Download Start Date"."Week"" saw-sql:aggregationRule="none" saw-sql:aggregationType="nonAgg" saw-sql:tableHeading="Download Start Date" saw-sql:columnHeading="Week" saw-sql:columnID="ce8ad46904662e816"/>
<xsd:element name="C5" type="xsd:string" minOccurs="1" maxOccurs="1" saw-sql:type="varchar" saw-sql:sqlFormula=""CBR"."Download Start Date"."Month"" saw-sql:displayFormula=""Download Start Date"."Month"" saw-sql:aggregationRule="none" saw-sql:aggregationType="nonAgg" saw-sql:tableHeading="Download Start Date" saw-sql:columnHeading="Month" saw-sql:columnID="ca19e13040145f84b"/>
<xsd:element name="C6" type="xsd:string" minOccurs="1" maxOccurs="1" saw-sql:type="varchar" saw-sql:sqlFormula=""CBR"."Download Start Date"."Year"" saw-sql:displayFormula=""Download Start Date"."Year"" saw-sql:aggregationRule="none" saw-sql:aggregationType="nonAgg" saw-sql:tableHeading="Download Start Date" saw-sql:columnHeading="Year" saw-sql:columnID="cd55ae47d83c209a6"/>
<xsd:element name="C7" type="xsd:string" minOccurs="0" maxOccurs="1" saw-sql:type="varchar" saw-sql:sqlFormula=""CBR"."Customer Segment"."Customer Segment"" saw-sql:displayFormula=""Customer Segment"."Customer Segment"" saw-sql:aggregationRule="none" saw-sql:aggregationType="nonAgg" saw-sql:tableHeading="Customer Segment" saw-sql:columnHeading="Customer Segment" saw-sql:columnID="cebaa8810a3cb8064"/>
<xsd:element name="C8" type="xsd:string" minOccurs="1" maxOccurs="1" saw-sql:type="varchar" saw-sql:sqlFormula=""CBR"."Customer Contact"."Customer Type"" saw-sql:displayFormula=""Customer Contact"."Customer Type"" saw-sql:aggregationRule="none" saw-sql:aggregationType="nonAgg" saw-sql:tableHeading="Customer Contact" saw-sql:columnHeading="Customer Type" saw-sql:columnID="c602557f90d4ad9da"/>
<xsd:element name="C9" type="xsd:string" minOccurs="0" maxOccurs="1" saw-sql:type="varchar" saw-sql:sqlFormula=""CBR"."Dnb Organization"."Business Name"" saw-sql:displayFormula=""Dnb Organization"."Business Name"" saw-sql:aggregationRule="none" saw-sql:aggregationType="nonAgg" saw-sql:tableHeading="Dnb Organization" saw-sql:columnHeading="Business Name" saw-sql:columnID="c5b35569f06cec8ea"/>
<xsd:element name="C10" type="xsd:string" minOccurs="0" maxOccurs="1" saw-sql:type="varchar" saw-sql:sqlFormula=""CBR"."Dnb Organization"."Country Region"" saw-sql:displayFormula=""Dnb Organization"."Country Region"" saw-sql:aggregationRule="none" saw-sql:aggregationType="nonAgg" saw-sql:tableHeading="Dnb Organization" saw-sql:columnHeading="Country Region" saw-sql:columnID="c522eb1b6f5c0fe4d"/>
<xsd:element name="C11" type="xsd:string" minOccurs="0" maxOccurs="1" saw-sql:type="varchar" saw-sql:sqlFormula=""CBR"."Dnb Organization"."Country Geo"" saw-sql:displayFormula=""Dnb Organization"."Country Geo"" saw-sql:aggregationRule="none" saw-sql:aggregationType="nonAgg" saw-sql:tableHeading="Dnb Organization" saw-sql:columnHeading="Country Geo" saw-sql:columnID="c1874f494099b9641"/>
<xsd:element name="C12" type="xsd:string" minOccurs="0" maxOccurs="1" saw-sql:type="varchar" saw-sql:sqlFormula=""CBR"."Dnb Organization"."Iso Country Name"" saw-sql:displayFormula=""Dnb Organization"."Iso Country Name"" saw-sql:aggregationRule="none" saw-sql:aggregationType="nonAgg" saw-sql:tableHeading="Dnb Organization" saw-sql:columnHeading="Iso Country Name" saw-sql:columnID="ceb5291bac5a83a51"/>
<xsd:element name="C13" type="xsd:string" minOccurs="0" maxOccurs="1" saw-sql:type="varchar" saw-sql:sqlFormula=""CBR"."Customer Contact"."City Name"" saw-sql:displayFormula=""Customer Contact"."City Name"" saw-sql:aggregationRule="none" saw-sql:aggregationType="nonAgg" saw-sql:tableHeading="Customer Contact" saw-sql:columnHeading="City Name" saw-sql:columnID="c3fcf2b0a26f2d882"/>
<xsd:element name="C14" type="xsd:string" minOccurs="0" maxOccurs="1" saw-sql:type="varchar" saw-sql:sqlFormula=""CBR"."Customer Contact"."Country"" saw-sql:displayFormula=""Customer Contact"."Country"" saw-sql:aggregationRule="none" saw-sql:aggregationType="nonAgg" saw-sql:tableHeading="Customer Contact" saw-sql:columnHeading="Country" saw-sql:columnID="c62a616664e32a709"/>
<xsd:element name="C15" type="xsd:string" minOccurs="1" maxOccurs="1" saw-sql:type="varchar" saw-sql:sqlFormula=""CBR"."Customer Contact"."Last Name"" saw-sql:displayFormula=""Customer Contact"."Last Name"" saw-sql:aggregationRule="none" saw-sql:aggregationType="nonAgg" saw-sql:tableHeading="Customer Contact" saw-sql:columnHeading="Last Name" saw-sql:columnID="c0651d86673c4e74d"/>
<xsd:element name="C16" type="xsd:string" minOccurs="0" maxOccurs="1" saw-sql:type="varchar" saw-sql:sqlFormula=""CBR"."Customer Contact"."First Name"" saw-sql:displayFormula=""Customer Contact"."First Name"" saw-sql:aggregationRule="none" saw-sql:aggregationType="nonAgg" saw-sql:tableHeading="Customer Contact" saw-sql:columnHeading="First Name" saw-sql:columnID="c9dfc11cb810a623d"/>
<xsd:element name="C17" type="xsd:string" minOccurs="1" maxOccurs="1" saw-sql:type="varchar" saw-sql:sqlFormula=""CBR"."Customer Contact"."Email Address"" saw-sql:displayFormula=""Customer Contact"."Email Address"" saw-sql:aggregationRule="none" saw-sql:aggregationType="nonAgg" saw-sql:tableHeading="Customer Contact" saw-sql:columnHeading="Email Address" saw-sql:columnID="cf7152d03553d4987"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
<R>
<C0>/software/vcm/56/VCM-OS-Provisioning-Server-5.6.iso</C0>
<C1>VMware vCenter Configuration Manager 5.6.0</C1>
<C2>14Q1</C2>
<C3>2014-01-02</C3>
<C4>W01</C4>
<C5>JANUARY</C5>
<C6>2014</C6>
<C7>Enterprise</C7>
<C8>ESTABLISHED</C8>
<C9>XCHANGING PLC</C9>
<C10>NORTHERN EUROPE</C10>
<C11>EMEA</C11>
<C12>UNITED KINGDOM</C12>
<C13>BASILDON</C13>
<C14>GB</C14>
<C15>Delves</C15>
<C16>Craig</C16>
<C17>craig.delves@xchanging.com</C17>
</R></RS>'
declare @SQL nvarchar(max) = ''
declare @Col nvarchar(max) = ', T.N.value(''[COLNAME][1]'', ''varchar(100)'') as [COLNAME]'
;WITH XMLNAMESPACES( DEFAULT 'urn:schemas-microsoft-com:xml-analysis:rowset')
select @SQL = @SQL + replace(@Col, '[COLNAME]', T.N.value('local-name(.)', 'sysname'))
from @XML.nodes('//R') as T(N)
set @SQL = 'select '+stuff(@SQL, 1, 2, '')+' from @XML.nodes(''//R'') as T(N)'
exec sp_executesql @SQL, N'@XML xml', @XML
当我运行查询时,它什么都不返回。声明命名空间时出现问题。找到解决方案。
答案 0 :(得分:1)
您正在构建动态SQL语句,但with xmlnamespaces
部分不会使其进入动态SQL查询。
请注意,XPath区分大小写,因此请使用//R
代替//r
:
; with xmlnamespaces(default 'urn:schemas-microsoft-com:xml-analysis:rowset')
select t.n.value('C0[1]', 'varchar(100)') as r
from @xml.nodes('//R') as t(n)
打印:
/software/vcm/56/VCM-OS-Provisioning-Server-5.6.iso
要查找所有列的列表,您可以:
; with xmlnamespaces(default 'urn:schemas-microsoft-com:xml-analysis:rowset')
select T.N.value('local-name(.)', 'sysname')
from @XML.nodes('//R/child::*') as T(N)
打印:
C0
C1
C2
C3
...