请尝试以下解决方案。我们在这里所做的称为切碎,即将XML 转换为矩形/关系格式。
因为没有提供DDL 和样本数据总体,所以我从臀部射击。
由于缺少根元素,提供的 XML 格式不正确,但 SQL Server 允许处理 XML 片段。
.nodes()
我们正在使用XQuery及其.value()
方法。
SQL,方法 #1
-- DDL and sample data population, start
DECLARE @xml XML =
N'<DW>
<KS>
<KeyInfo Name="IlluSetting">
<KeyTypeValue>Text</KeyTypeValue>
<ValueString>yDipol90</ValueString>
</KeyInfo>
<KeyInfo Name="IlluSetting2">
<KeyTypeValue>Text</KeyTypeValue>
<ValueString>yDipol</ValueString>
</KeyInfo>
</KS>
<MDESC>Tx [mrad]</MDESC>
<MNUMBER>0.12102</MNUMBER>
</DW>
<DW>
<KS>
<KeyInfo Name="IlluSetting3">
<KeyTypeValue>Text</KeyTypeValue>
<ValueString>yDipol80</ValueString>
</KeyInfo>
</KS>
<MDESC>Ty [mrad]</MDESC>
<MNUMBER>0.12102</MNUMBER>
</DW>';
-- DDL and sample data population, end
SELECT c.value('KeyInfo[1]/@Name', 'VARCHAR(30)') AS name1
, c.value('(KeyInfo[1]/ValueString/text())[1]', 'VARCHAR(30)') AS ValueString1
, COALESCE(c.value('KeyInfo[2]/@Name', 'VARCHAR(30)'), '') AS name2
, COALESCE(c.value('(KeyInfo[2]/ValueString/text())[1]', 'VARCHAR(30)'), '') AS ValueString2
FROM @xml.nodes('/DW/KS') AS t(c);
输出
+--------------+--------------+--------------+--------------+
| name1 | ValueString1 | name2 | ValueString2 |
+--------------+--------------+--------------+--------------+
| IlluSetting | yDipol90 | IlluSetting2 | yDipol |
| IlluSetting3 | yDipol80 | | |
+--------------+--------------+--------------+--------------+
SQL,方法 #2
DECLARE @CrLf CHAR(2) = CHAR(13) + CHAR(10)
, @tokenCounter INT
, @i INT = 1;
-- Calculate max number of tokens in the <KS>
SET @tokenCounter = (SELECT MAX(c.value('count(KeyInfo)', 'INT'))
FROM @xml.nodes('/DW/KS') AS t(c));
DECLARE @SQL NVARCHAR(MAX) = 'SELECT ';
WHILE @i <= @tokenCounter BEGIN
SET @SQL += IIF(@i>1,', ','') + 'COALESCE(c.value(''KeyInfo[' + CAST(@i AS VARCHAR(3)) + ']/@Name'', ''VARCHAR(30)''), '''') AS NAME' + CAST(@i AS VARCHAR(3)) + @CrLf
SET @SQL += ', COALESCE(c.value(''(KeyInfo[' + CAST(@i AS VARCHAR(3)) + ']/ValueString/text())[1]'', ''VARCHAR(30)''), '''') AS ValueString' + CAST(@i AS VARCHAR(3)) + @CrLf
SET @i += 1
END
SET @SQL += 'FROM @xml.nodes(''/DW/KS'') AS t(c);';
-- just to see it
PRINT @sql;
-- we are ready at this point
EXEC sp_executesql @stmt = @SQL, @params = N'@xml xml', @xml = @xml;