OPENXML不返回数据,没有错误消息

时间:2018-08-17 00:28:17

标签: sql-server xml openxml sql-server-2016 openrowset

我以前从未使用过XML,但是受过将文档导入SQL Server表的任务。作为测试,我一直在使用我在网上找到的示例,现在,我仅尝试检索单个列:

DECLARE @X XML

SELECT @X=MC2016
FROM OPENROWSET (BULK 'C:\TEMP\Detail_2016.xml', SINGLE_BLOB) AS MEDPOP_2016(MC2016)

DECLARE @hdoc int

EXEC SP_XML_PREPAREDOCUMENT @hdoc OUTPUT, @X

select * 
from openxml (@hdoc, '/Results/Record', 1)
with (
    MemberID varchar(10)
    )

EXEC SP_XML_REMOVEDOCUMENT @hdoc

当我在SSMS中执行此操作时,查询完成,但是即使知道有成千上万的记录,也得到零行输出。

我正在运行SQL Server2016。此处包含的XML文件太大,但这是第一条记录(已标识):

<?xml version="1.0" encoding="utf-8"?>
<Results xmlns="http://www.dxcg.com/Results.xsd">
<Record MemberID="0012345" DOB="1917-09-09" Age="100" Gender="M" Child="0" Elderly="1" Male="1" AgeGender="34" NoHCC="false" NoValid="false" NoDiag="false" NoRxGroup="true" NoValidRx="false" ELG1="12" ELIGF1="1" ECAT="-1" MCAID="0" OREC="0" EXP1="33.78" ExpRx1="0.00">
  <Groups>
    <Group Name="FSC" Value="99" />
  </Groups>
  <Period Name="Base">
    <Classifier Type="Standard">
      <DxGs>
        <DxG ID="887" />
        <DxG ID="638" />
        <DxG ID="910" />
        <DxG ID="911" />
        <DxG ID="716" />
        <DxG ID="534" />
        <DxG ID="530" />
        <DxG ID="61" />
      </DxGs>
      <CCs>
        <CC ID="266" HierarchyY1="true" HierarchyY2="true" />
        <CC ID="230" HierarchyY1="true" HierarchyY2="true" />
        <CC ID="19" HierarchyY1="true" HierarchyY2="true" />
        <CC ID="349" HierarchyY1="false" HierarchyY2="true" />
        <CC ID="231" HierarchyY1="false" HierarchyY2="false" />
        <CC ID="347" HierarchyY1="true" HierarchyY2="false" />
        <CC ID="293" HierarchyY1="true" HierarchyY2="true" />
      </CCs>
      <RCCs>
        <RCC ID="77" HierarchyY1="true" HierarchyY2="true" />
        <RCC ID="62" HierarchyY1="true" HierarchyY2="true" />
        <RCC ID="2" HierarchyY1="true" HierarchyY2="true" />
        <RCC ID="103" HierarchyY1="true" HierarchyY2="true" />
        <RCC ID="87" HierarchyY1="true" HierarchyY2="true" />
      </RCCs>
      <ACCs>
        <ACC ID="19" HierarchyY1="true" HierarchyY2="true" />
        <ACC ID="16" HierarchyY1="true" HierarchyY2="true" />
        <ACC ID="2" HierarchyY1="true" HierarchyY2="true" />
        <ACC ID="27" HierarchyY1="true" HierarchyY2="true" />
        <ACC ID="22" HierarchyY1="true" HierarchyY2="true" />
      </ACCs>
      <RxGs />
      <ARxGs />
    </Classifier>
  </Period>
  <DCGs>
    <DCG ID="2" Value="0.5" />
  </DCGs>
  <ADCGs>
    <ADCG ID="2" Value="0.5" />
  </ADCGs>
  <Preds>
    <Pred ID="122" Value="1.6393262810427782" />
    <Pred ID="2" Value="0.55220631651432173">
      <RiskDriver Label="" HCC="293" Contribution="68.537299593489152" />
      <RiskDriver Label="" HCC="230" Contribution="18.964637260918547" />
      <RiskDriver Label="" HCC="347" Contribution="5.12631266098848" />
      <RiskDriver Label="" HCC="266" Contribution="4.43513850335468" />
      <RiskDriver Label="" HCC="19" Contribution="2.936611981249146" />
    </Pred>
  </Preds>
</Record>

最终,我想将整个记录加载到表中,但是到目前为止,我什至无法读取单个列,也没有收到任何错误消息来指示我要寻找的方向。

我在这里想念什么?谢谢。

最好的问候, 科里

1 个答案:

答案 0 :(得分:0)

从注释中提供的链接中的代码开始,您可以使用以下代码:

DECLARE @X XML

SELECT @X=MC2016
FROM OPENROWSET (BULK 'c:\TEMP\Detail_2016.xml', SINGLE_BLOB) AS MEDPOP_2016(MC2016)

DECLARE @hdoc int

EXEC SP_XML_PREPAREDOCUMENT @hdoc OUTPUT, @X, '<myNs xmlns:ns1="http://www.dxcg.com/Results.xsd"/>'

;WITH XMLNAMESPACES ('http://www.dxcg.com/Results.xsd' as ns1) 
select * 
from openxml (@hdoc, '/ns1:Results/ns1:Record', 1)
with (
    MemberID varchar(10)
    )

结果:

enter image description here

注意:您发布的xml可能缺少根元素的结束标记 </Results>