<legend id='HIzwC'><style id='HIzwC'><dir id='HIzwC'><q id='HIzwC'></q></dir></style></legend>
  • <small id='HIzwC'></small><noframes id='HIzwC'>

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

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

        如何选择XML中的所有列

        时间:2023-06-07

            • <legend id='17lpy'><style id='17lpy'><dir id='17lpy'><q id='17lpy'></q></dir></style></legend>
            • <tfoot id='17lpy'></tfoot>

              <small id='17lpy'></small><noframes id='17lpy'>

                  <tbody id='17lpy'></tbody>
                <i id='17lpy'><tr id='17lpy'><dt id='17lpy'><q id='17lpy'><span id='17lpy'><b id='17lpy'><form id='17lpy'><ins id='17lpy'></ins><ul id='17lpy'></ul><sub id='17lpy'></sub></form><legend id='17lpy'></legend><bdo id='17lpy'><pre id='17lpy'><center id='17lpy'></center></pre></bdo></b><th id='17lpy'></th></span></q></dt></tr></i><div id='17lpy'><tfoot id='17lpy'></tfoot><dl id='17lpy'><fieldset id='17lpy'></fieldset></dl></div>
                  <bdo id='17lpy'></bdo><ul id='17lpy'></ul>
                  本文介绍了如何选择XML中的所有列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..
                  DECLARE @x AS XML
                  SET @x = '<Table1><c1><![CDATA[1]]></c1><c2><![CDATA[Sample Record]]></c2><c3><![CDATA[Test Data]]></c3></Table1>'
                  SELECT * FROM @x.nodes('/Table1')
                  

                  我想选择所有列而不定义列名(使用 *)

                  I want to select all columns without defining the column name (using *)

                  推荐答案

                  没有与 select * 等效的东西.最接近的是获取一列中的节点值和另一列中的节点名称.

                  There is no equivalent to select *. The closest you can get is to get the node values in one column and the node names in another column.

                  select T.X.value('local-name(.)', 'nvarchar(max)') as ColName,
                         T.X.value('text()[1]', 'nvarchar(max)') as ColValue
                  from @x.nodes('Table1/*') as T(X)
                  

                  结果:

                  ColName              ColValue
                  -------------------- --------------------
                  c1                   1
                  c2                   Sample Record
                  c3                   Test Data
                  

                  如果您希望节点名称作为输出中的列名称,您必须构造一个查询,指定要从中获取值的节点,并且您必须指定用于该列的列别名.

                  If you want the node names as column names in the output you have to construct a query that specifies the node to get the value from and you have to specify the column alias to use for that column.

                  select T.X.value('(c1/text())[1]', 'nvarchar(max)') as c1,
                         T.X.value('(c2/text())[1]', 'nvarchar(max)') as c2,
                         T.X.value('(c3/text())[1]', 'nvarchar(max)') as c3
                  from @x.nodes('Table1') as T(X)
                  

                  c1                   c2                   c3
                  -------------------- -------------------- --------------------
                  1                    Sample Record        Test Data
                  

                  可以使用 XML 作为源动态构建和执行该查询.

                  That query can be built and executed dynamically using the XML as the source.

                  declare @SQL nvarchar(max) =
                  'select '+stuff((select ',T.X.value(''('+C.Name+'/text())[1]'', ''nvarchar(max)'') as '+C.Name
                                   from @x.nodes('Table1/*') as T(X)
                                     cross apply (select T.X.value('local-name(.)', 'nvarchar(max)')) as C(Name)
                                   for xml path(''), type).value('text()[1]', 'nvarchar(max)'), 1, 1, '')+
                  ' from @x.nodes(''Table1'') as T(X)'
                  
                  exec sp_executesql @SQL, N'@x xml', @x
                  

                  这篇关于如何选择XML中的所有列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在 SQL Server 2008 中查询 XML 列 下一篇:SELECT FOR XML 查询速度慢吗?

                  相关文章

                  最新文章

                  <legend id='bSQYv'><style id='bSQYv'><dir id='bSQYv'><q id='bSQYv'></q></dir></style></legend>

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

                      • <bdo id='bSQYv'></bdo><ul id='bSQYv'></ul>

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