1. <tfoot id='vzWBF'></tfoot>

      1. <legend id='vzWBF'><style id='vzWBF'><dir id='vzWBF'><q id='vzWBF'></q></dir></style></legend>
        <i id='vzWBF'><tr id='vzWBF'><dt id='vzWBF'><q id='vzWBF'><span id='vzWBF'><b id='vzWBF'><form id='vzWBF'><ins id='vzWBF'></ins><ul id='vzWBF'></ul><sub id='vzWBF'></sub></form><legend id='vzWBF'></legend><bdo id='vzWBF'><pre id='vzWBF'><center id='vzWBF'></center></pre></bdo></b><th id='vzWBF'></th></span></q></dt></tr></i><div id='vzWBF'><tfoot id='vzWBF'></tfoot><dl id='vzWBF'><fieldset id='vzWBF'></fieldset></dl></div>

          <bdo id='vzWBF'></bdo><ul id='vzWBF'></ul>

        <small id='vzWBF'></small><noframes id='vzWBF'>

      2. 带有 SQL 的 XML 节点

        时间:2023-06-06
        <i id='sPx8R'><tr id='sPx8R'><dt id='sPx8R'><q id='sPx8R'><span id='sPx8R'><b id='sPx8R'><form id='sPx8R'><ins id='sPx8R'></ins><ul id='sPx8R'></ul><sub id='sPx8R'></sub></form><legend id='sPx8R'></legend><bdo id='sPx8R'><pre id='sPx8R'><center id='sPx8R'></center></pre></bdo></b><th id='sPx8R'></th></span></q></dt></tr></i><div id='sPx8R'><tfoot id='sPx8R'></tfoot><dl id='sPx8R'><fieldset id='sPx8R'></fieldset></dl></div>
          • <tfoot id='sPx8R'></tfoot>
            <legend id='sPx8R'><style id='sPx8R'><dir id='sPx8R'><q id='sPx8R'></q></dir></style></legend>

              <tbody id='sPx8R'></tbody>

              <small id='sPx8R'></small><noframes id='sPx8R'>

                  <bdo id='sPx8R'></bdo><ul id='sPx8R'></ul>
                  本文介绍了带有 SQL 的 XML 节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我有以下 XML 数据,并希望按如下方式在 KS 中获取数据:

                  I have the following XML data and would like to get data inside KS as row as follows:

                  <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>
                  

                  有什么办法可以得到一个具有以下输出的表格:

                  Is there any way to get a Table with the following output:

                  Name            ValueString     Name            ValueString
                  -----------------------------------------------------------
                  IlluSetting     yDipol90        IlluSetting2    yDipol
                  IlluSetting3    yDipol80    
                  

                  表示<KS>...</KS>里面的数据会排成一行显示

                  which means that the data inside <KS>... </KS> will be shown in a row

                  非常感谢

                  推荐答案

                  请尝试以下解决方案.我们在这里所做的称为粉碎,即将 XML 转换为矩形/关系格式.

                  Please try the following solution. What we are doing here is called shredding, i.e. converting XML into rectangular/relational format.

                  我正在拍摄,因为没有提供DDL 和样本数据群.

                  I am shooting from the hip because DDL and sample data population were not provided.

                  由于缺少根元素,提供的 XML 格式不正确,但 SQL Server 允许处理 XML 片段.

                  The provided XML is not well-formed due to missing root element, but SQL Server allows to handle XML fragments.

                  我们正在使用 XQuery 及其 .nodes().value() 方法.

                  We are using XQuery and its .nodes() and .value() methods.

                  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;
                  

                  这篇关于带有 SQL 的 XML 节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:XQuery 检索值 下一篇:使用 Xquery 检索 xml 属性

                  相关文章

                  最新文章

                  <tfoot id='MaFxj'></tfoot>
                  • <bdo id='MaFxj'></bdo><ul id='MaFxj'></ul>

                    <i id='MaFxj'><tr id='MaFxj'><dt id='MaFxj'><q id='MaFxj'><span id='MaFxj'><b id='MaFxj'><form id='MaFxj'><ins id='MaFxj'></ins><ul id='MaFxj'></ul><sub id='MaFxj'></sub></form><legend id='MaFxj'></legend><bdo id='MaFxj'><pre id='MaFxj'><center id='MaFxj'></center></pre></bdo></b><th id='MaFxj'></th></span></q></dt></tr></i><div id='MaFxj'><tfoot id='MaFxj'></tfoot><dl id='MaFxj'><fieldset id='MaFxj'></fieldset></dl></div>
                  1. <legend id='MaFxj'><style id='MaFxj'><dir id='MaFxj'><q id='MaFxj'></q></dir></style></legend>

                    <small id='MaFxj'></small><noframes id='MaFxj'>