在MSSQL中使用OPENXML我只从第一列

时间:2017-09-01 14:36:48

标签: sql sql-server sql-server-openxml

我遇到了一个问题,我将XML文件的内容导入MSSQL。但是我只在第一列中获取数据。

我的XML的样子:

<ArrayOfPseudoAnimal xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <PseudoAnimal>
    <ID>a77b121e-08ec-4f1a-a997-387a8e0652d1</ID>
    <AnimalID>UK168277300003</AnimalID>
    <HoldingID>1e988796-2987-4d3f-964b-f12ae582d1bf</HoldingID>
    <DateIn>2015-06-24T00:00:00</DateIn>
    <DOB>2010-12-17T00:00:00</DOB>
    <Sex>Female</Sex>
    <BreedCode>HO</BreedCode>
    <ImportDate>2013-06-28T00:00:00</ImportDate>
    <DataDate xsi:nil="true" />
    <MilkRecordManagementID>3</MilkRecordManagementID>
    <MilkRecordOfficialID>168277300003</MilkRecordOfficialID>
    <MilkRecordUniqueID>4614659</MilkRecordUniqueID>
    <ManagementID>3</ManagementID>
    <NoOfCalves>3</NoOfCalves>
  </PseudoAnimal>
  <PseudoAnimal>
    <ID>2eff9a24-f0e5-4a4d-ad4d-e2f15dc6565a</ID>
    <AnimalID>UK168277700007</AnimalID>
    <HoldingID>1e988796-2987-4d3f-964b-f12ae582d1bf</HoldingID>
    <DateIn>2010-12-22T00:00:00</DateIn>
    <DOB>2010-12-22T00:00:00</DOB>
    <Sex>Female</Sex>
    <BreedCode>HO</BreedCode>
    <ImportDate>2013-06-28T00:00:00</ImportDate>
    <DataDate xsi:nil="true" />
    <MilkRecordManagementID>7</MilkRecordManagementID>
    <MilkRecordOfficialID>168277700007</MilkRecordOfficialID>
    <MilkRecordUniqueID>4614681</MilkRecordUniqueID>
    <ManagementID>7</ManagementID>
    <NoOfCalves>4</NoOfCalves>
  </PseudoAnimal>
</ArrayOfPseudoAnimal>

以下是我正在使用的SQL

DECLARE @XMLData XML

SELECT @XMLData = CONVERT(XML, BulkColumn)
FROM OPENROWSET(BULK 'G:\Animals.xml', SINGLE_BLOB) AS x

DECLARE @hDoc AS INT, @SQL NVARCHAR(MAX)

EXEC sp_xml_preparedocument @hDoc OUTPUT, @XMLData

SELECT *
FROM OPENXML(@hDoc, 'ArrayOfPseudoAnimal/PseudoAnimal', 2)   
       WITH (
                ID UNIQUEIDENTIFIER
                , animalID NVARCHAR(50)
                , holdingID UNIQUEIDENTIFIER
                , dateIn DATETIME
                , dob DATETIME
                , methodOfDisposal NVARCHAR(255)
                , sex NVARCHAR(10)
                , breedCode NVARCHAR(50)
                , importDate DATETIME
                , dataDate DATETIME
                , milkRecordManagementID NVARCHAR(50)
                , milkRecordOfficialID NVARCHAR(50)
                , milkRecordUniqueID NVARCHAR(50)
                , managementID NVARCHAR(50)
                , managementGroup UNIQUEIDENTIFIER
                , noOfCalves INT
            )

但是当我运行它时,结果窗格中唯一的列是ID列,Management Studio告诉我Query已成功执行。

关于我做错了什么建议?

0 个答案:

没有答案