从 SQL Server 中的 XML 批量插入
我正在尝试使用 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。据我了解,这是合理的,因为
是
的子节点。而且我并不是专门阅读
节点。请帮忙。
I am trying to insert data using a XML in SQL Server. The XML I am using is
<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>
I want to shred this XML in a temp table. I tried using
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
But I am getting Null in Offencecode and vehicle class. And as I understand, this is justified as <offenceCode>
is a child node of <offenceEntity>
. And I am not exclusively reading <offenceEntity>
node. Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您的 XML,您可以使用此 XQuery
SELECT
从 XML 中提取您的项目:将
@input
替换为保存 XML 的变量或列(我使用过@input作为我的测试中的测试台)。输出如下所示:
当然,您也可以执行
INSERT INTO ....
并使用此SELECT
的输出作为要插入的值。更新:如果您的 XML 列包含多个条目(
/ArrayOfInfringementEntity
内的/InfringementEntity
),则需要使用SELECT< /代码>像这样:
Based on your XML, you could use this XQuery
SELECT
to extract your items from the XML:Replace
@input
with the variable or column that holds your XML (I've used @input as a test bed in my tests).The output looks like this:
And of course, you can also do an
INSERT INTO ....
and use the output from thisSELECT
as the values to insert.Update: if your XML column contains multiple entries (of
/InfringementEntity
inside the/ArrayOfInfringementEntity
), you need to use aSELECT
like this: