我有一个包含 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 循环完成的,但我真的不知道如何实现它。有人可以帮我解决这个问题,甚至可以将其设为存储过程吗?