从SQL Server中的XML批量插入

时间:2011-01-11 17:15:43

标签: .net sql-server xml sql-server-2005 bulkinsert

我正在尝试使用SQL Server中的XML插入数据。我正在使用的XML是

<ArrayOfInfringementEntity xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <InfringementEntity>
        <infringementNumber>12345678911</infringementNumber>
        <issueAgency>017</issueAgency>
        <infringementType>1A</infringementType>
        <infringementStatus>0</infringementStatus>
        <batchRecordId>0</batchRecordId>
        <incidentDate xsi:nil="true" />
        <infringementSource>OTS</infringementSource>
        <TypeOfNotice>0</TypeOfNotice>
        <offenceEntity>
            <offenceCode>7777</offenceCode>
            <offenceDate>1999-05-31T00:00:00</offenceDate>
            <offenceTime>121212</offenceTime>
            <offenceLocation>ST56789</offenceLocation>
            <offenceOwnerType>0</offenceOwnerType>
            <offenceSuburb>SOUTH YARRA</offenceSuburb>
            <site>ST56789</site>
            <detectedSpeed>70</detectedSpeed>
            <allegedSpeed>60</allegedSpeed>
            <permittedSpeed>50</permittedSpeed>
            <timeInRedLight>40</timeInRedLight>
            <tollAmount>140</tollAmount>
            <enforcementAllowance>310</enforcementAllowance>
            <lookUpFee>510</lookUpFee>
            <invoiceFee>130</invoiceFee>
        </offenceEntity>
        <vehicleEntity>
            <vehicleClass>2</vehicleClass>
            <vehicleMake>BMW</vehicleMake>
            <vehicleModel>FOUR WHEELER</vehicleModel>
            <bodyType>HEAVY</bodyType>
            <primaryColour>GRN</primaryColour>
            <manufactureYear>2010</manufactureYear>
            <gvm>111</gvm>
            <gcm>210</gcm>
            <registrationNumber>CBD-1111</registrationNumber>
            <registrationState>VIC</registrationState>
        </vehicleEntity>
        <obligationNumber>obligation1</obligationNumber>
        <isDebtorDeceased>false</isDebtorDeceased>
    </InfringementEntity>
</ArrayOfInfringementEntity>

我想在临时表中粉碎这个XML。我尝试使用

create table #InfTemp

(infringementNumber Varchar(10),issueAgency varchar(5),infringementType varchar(5), offenceCode int,vehicleClass int,obligationNumber varchar(11)
)

Insert into #InfTemp
   SELECT  PLIxml.infringementNumber, PLIxml.issueAgency,PLIxml.infringementType,  
    PLIxml.offenceCode , PLIxml.vehicleClass ,PLIxml.obligationNumber 
  FROM  OPENXML (@output, 'ArrayOfInfringementEntity/InfringementEntity',2)   
  WITH
  (  infringementNumber Varchar(10),issueAgency varchar(5),infringementType varchar(5),offenceCode int,vehicleClass int,obligationNumber varchar(11)
   ) PLIxml 

但我在Offencecode和车辆类中获得Null。据我了解,这是合理的,因为<offenceCode><offenceEntity>的子节点。我并不是专门阅读<offenceEntity>节点。请帮忙。

2 个答案:

答案 0 :(得分:3)

根据您的XML,您可以使用此XQuery SELECT从XML中提取项目:

select
    @input.value('(/ArrayOfInfringementEntity/InfringementEntity/infringementNumber)[1]', 'VARCHAR(10)') 'InfringementNumber',
    @input.value('(/ArrayOfInfringementEntity/InfringementEntity/issueAgency)[1]', 'VARCHAR(5)') 'Issue Agency',
    @input.value('(/ArrayOfInfringementEntity/InfringementEntity/infringementType)[1]', 'VARCHAR(5)') 'Infringement Type',
    @input.value('(/ArrayOfInfringementEntity/InfringementEntity/offenceEntity/offenceCode)[1]', 'INT') 'Offence Code',
    @input.value('(/ArrayOfInfringementEntity/InfringementEntity/vehicleEntity/vehicleClass)[1]', 'INT') 'Vehicle Class',
    @input.value('(/ArrayOfInfringementEntity/InfringementEntity/obligationNumber)[1]', 'VARCHAR(11)') 'Obligation Number'

@input替换为包含XML的变量或列(我在测试中使用@input作为测试床)。

输出如下:

InfringementNumber  Issue Agency    Infringement Type   Offence Code    Vehicle Class   Obligation Number
1234567891              017                1A              7777            2                obligation1

当然,您也可以执行INSERT INTO ....并使用此SELECT的输出作为要插入的值。

更新:如果您的XML列包含多个条目(/InfringementEntity内的/ArrayOfInfringementEntity),则需要使用SELECT,如下所示:

SELECT
    InfrEntity.value('(infringementNumber)[1]', 'VARCHAR(10)') 'InfringementNumber',
    InfrEntity.value('(issueAgency)[1]', 'VARCHAR(5)') 'Issue Agency',
    InfrEntity.value('(infringementType)[1]', 'VARCHAR(5)') 'Infringement Type',
    InfrEntity.value('(offenceEntity/offenceCode)[1]', 'INT') 'Offence Code',
    InfrEntity.value('(vehicleEntity/vehicleClass)[1]', 'INT') 'Vehicle Class',
    InfrEntity.value('(obligationNumber)[1]', 'VARCHAR(11)') 'Obligation Number'
from
    (yourXMLcolumn).nodes('/ArrayOfInfringementEntity/InfringementEntity') as ArrInfr(InfrEntity)

答案 1 :(得分:1)

您可以使用SqlBulkCopy将XML快速导入MS Sql-Server。以下是一个示例:blogs.microsoft/importing-large-xml-files-to-sql-server-using-sqlbulkcopy