0

我有一个包含 3 个 XML 的临时表:

CREATE TABLE #XMLwithOpenXML
(
    Id INT IDENTITY PRIMARY KEY,
    XMLData XML,
    LoadedDateTime DATETIME
);
go

INSERT INTO #XMLwithOpenXML(XMLData, LoadedDateTime) 
    SELECT 
        CONVERT(XML, BulkColumn) AS BulkColumn, GETDATE() 
    FROM 
        OPENROWSET(BULK 'D:\Test\Test1.xml', SINGLE_BLOB) AS x;

INSERT INTO #XMLwithOpenXML(XMLData, LoadedDateTime) 
    SELECT 
        CONVERT(XML, BulkColumn) AS BulkColumn, GETDATE() 
    FROM 
        OPENROWSET(BULK 'D:\Test\Test2.xml', SINGLE_BLOB) AS x;

INSERT INTO #XMLwithOpenXML(XMLData, LoadedDateTime) 
    SELECT 
        CONVERT(XML, BulkColumn) AS BulkColumn, GETDATE() 
    FROM 
        OPENROWSET(BULK 'D:\Test\Test2.xml', SINGLE_BLOB) AS x;
go

我正在尝试从中提取一些数据到另一个临时表中,并且使用以下代码成功地这样做了:

DECLARE @XML AS XML, @hDoc as int, @SQL nvarchar(max)

SELECT @XML = XMLData 
FROM #XMLwithOpenXML

SELECT @XML

EXEC sp_xml_preparedocument @hDoc OUTPUT, @XML

EXEC Rm #DocNameForCapsilonFieldId

SELECT
    DocTypeName = typeName, DataPointName, FieldId = FieldName, ValueData
INTO
    #DocNameForCapsilonFieldId
FROM
    OPENXML(@hDoc, 'documents/document/dataPoints/dataPoint/field/value')
    WITH
        (
            typeName [varchar](256) '../../../../typeName',
            DataPointName [varchar](256) '../../@name',
            FieldName [varchar](256) '../@name',
            ValueData [varchar](256) '../value'
        ) AS DocTypeName

EXEC sp_xml_removedocument @hDoc
go

但问题是它只拾取第一个 XML 并提取其数据。我想从所有 3 个 XML 中提取数据并将其存储#DocNameForCapsilonFieldId在数据库中或任何表中。我知道这是通过 While 循环完成的,但我真的不知道如何实现它。有人可以帮我解决这个问题,甚至可以将其设为存储过程吗?

4

2 回答 2

2

首先:FROM OPENXML与 SP 一起准备和删除文档,已过时,不应再使用(存在极少数例外)。

始终尝试使用XML 数据类型提供的本机 XML 方法。即.nodes(),在派生集中检索与多行一样的重复元素,并.value()获取标量值。

这并不是一个真正的新答案,更多的是对@AlwaysLearning 答案的补充。

向后导航(父轴,在您的示例中使用 multiple )../表现非常糟糕。APPLY更好地使用with的级联,在实际 XPath.nodes()上越走越深:

(DDL 和示例的学分:@AlwaysLearning 的答案)

drop table if exists #XMLwithOpenXML;
create table #XMLwithOpenXML (
  XMLData xml
);
insert #XMLwithOpenXML values
(N'<documents>
 <document>
  <typeName>Example1</typeName>
  <dataPoints>
   <dataPoint name="dp11">
    <field name="foo">
     <value>42</value>
    </field>
   </dataPoint>
   <dataPoint name="dp12">
    <field name="bar">
     <value>47</value>
    </field>
   </dataPoint>
  </dataPoints>
 </document>
</documents>'),
(N'<documents>
 <document>
  <typeName>Example2</typeName>
  <dataPoints>
   <dataPoint name="dp21">
    <field name="baz">
     <value>21</value>
    </field>
   </dataPoint>
   <dataPoint name="dp22">
    <field name="chaz">
     <value>22</value>
    </field>
   </dataPoint>
  </dataPoints>
 </document>
</documents>');

--查询:

SELECT A.doc.value('(typeName/text())[1]','nvarchar(max)') AS TypeName
      ,B.dp.value('@name','nvarchar(max)') AS DataPoint_Name
      ,C.fld.value('@name','nvarchar(max)') AS Field_Name
      ,C.fld.value('(value/text())[1]','int') AS Field_Value
FROM #XMLwithOpenXML t
CROSS APPLY XMLData.nodes('/documents/document') A(doc)
OUTER APPLY A.doc.nodes('dataPoints/dataPoint') B(dp)
OUTER APPLY B.dp.nodes('field') C(fld);

结果

TypeName    DataPoint_Name  Field_Name  Field_Value
Example1    dp11            foo         42
Example1    dp12            bar         47
Example2    dp21            baz         21
Example2    dp22            chaz        22
于 2020-04-17T14:57:09.423 回答
0

您应该能够在没有WHILE循环的情况下执行此操作 - 改用CROSS APPLYwithnodes()value()XML 函数,例如如下所示:

drop table if exists #DocNameForCapsilonFieldId;
create table #DocNameForCapsilonFieldId (
  DocTypeName nvarchar(256),
  DataPointName nvarchar(256),
  FieldId nvarchar(256),
  ValueData nvarchar(256)
);

drop table if exists #XMLwithOpenXML;
create table #XMLwithOpenXML (
  XMLData xml
);
insert #XMLwithOpenXML values
(N'<documents>
 <document>
  <typeName>Example1</typeName>
  <dataPoints>
   <dataPoint name="dp11">
    <field name="foo">
     <value>42</value>
    </field>
   </dataPoint>
   <dataPoint name="dp12">
    <field name="bar">
     <value>47</value>
    </field>
   </dataPoint>
  </dataPoints>
 </document>
</documents>'),
(N'<documents>
 <document>
  <typeName>Example2</typeName>
  <dataPoints>
   <dataPoint name="dp21">
    <field name="baz">
     <value>21</value>
    </field>
   </dataPoint>
   <dataPoint name="dp22">
    <field name="chaz">
     <value>22</value>
    </field>
   </dataPoint>
  </dataPoints>
 </document>
</documents>');

insert #DocNameForCapsilonFieldId (DocTypeName, DataPointName, FieldId, ValueData)
  select
    x.n.value(N'../../../../typeName[1]', N'nvarchar(256)'),
    x.n.value(N'../../@name[1]', N'nvarchar(256)'),
    x.n.value(N'../@name[1]', N'nvarchar(256)'),
    x.n.value(N'.', N'nvarchar(256)')
  from #XMLwithOpenXML
  cross apply XMLData.nodes(N'documents/document/dataPoints/dataPoint/field/value') x(n);

这会产生结果......

DocTypeName  DataPointName  FieldId  ValueData
Example1     dp11           foo      42
Example1     dp12           bar      47
Example2     dp21           baz      21
Example2     dp22           chaz     22
于 2020-04-17T05:00:59.050 回答