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

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

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

      <tfoot id='flxYX'></tfoot>

    1. <legend id='flxYX'><style id='flxYX'><dir id='flxYX'><q id='flxYX'></q></dir></style></legend>
    2. 创建 XML 文件时在 SQL 中联合

      时间:2023-06-06

            <bdo id='9lwVb'></bdo><ul id='9lwVb'></ul>
            <tfoot id='9lwVb'></tfoot>

          • <legend id='9lwVb'><style id='9lwVb'><dir id='9lwVb'><q id='9lwVb'></q></dir></style></legend>
                <tbody id='9lwVb'></tbody>

              <small id='9lwVb'></small><noframes id='9lwVb'>

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

              1. 本文介绍了创建 XML 文件时在 SQL 中联合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                我在创建 XML 文件的 SQL 查询中遇到了一些问题.我想做 UNION 它这个查询,但它不起作用.

                I got some problem with my SQL query which create a XML file. I want to do UNION it this query but it doesn't work.

                (SELECT 1 AS "ns0:kindOfItem",
                code AS "ns0:wholeCode",
                REPLACE(weight, ',', '.') AS "ns0:weight",
                1 AS "ns0:ammountOfNumbers",
                (SELECT price AS "ns0:value",
                'EUR' as "ns0:currency"
                FOR XML PATH ('ns0:sendedItems'), TYPE),
                (SELECT 
                'EUR' as "ns0:currency"
                FOR XML PATH ('ns0:present'), TYPE)
                FROM [PL].[dbo].[dk_documents] where id in (1,2,3)
                FOR XML PATH('test'))
                

                这个查询工作正常,但是当我尝试像这里这样执行 UNION 时:

                This query works fine but when I try to do UNION like here:

                (SELECT 1 AS "ns0:kindOfItem",
                code AS "ns0:wholeCode",
                REPLACE(weight, ',', '.') AS "ns0:weight",
                1 AS "ns0:ammountOfNumbers",
                (SELECT price AS "ns0:value",
                'EUR' as "ns0:currency"
                FOR XML PATH ('ns0:sendedItems'), TYPE),
                (SELECT 
                'EUR' as "ns0:currency"
                FOR XML PATH ('ns0:present'), TYPE)
                FROM [PL].[dbo].[dk_documents] where id in (1,2,3)
                
                UNION
                
                (SELECT 1 AS "ns0:kindOfItem",
                code AS "ns0:wholeCode",
                REPLACE(weight, ',', '.') AS "ns0:weight",
                1 AS "ns0:ammountOfNumbers",
                (SELECT price AS "ns0:value",
                'EUR' as "ns0:currency"
                FOR XML PATH ('ns0:sendedItems'), TYPE),
                (SELECT 
                'EUR' as "ns0:currency"
                FOR XML PATH ('ns0:present'), TYPE)
                FROM [PL2].[dbo].[dk_documents] where id in (1,2,3)
                FOR XML PATH('test'))
                

                这个查询给我一个错误:

                This query give me an error:

                数据类型 xml 不能用作 UNION、INTERSECT 的操作数或 EXCEPT 运算符,因为它没有可比性.

                The data type xml cannot be used as an operand to the UNION, INTERSECT or EXCEPT operators because it is not comparable.

                推荐答案

                您可能对此感兴趣:

                请比较以下内容

                测试"一词出现在两个列表中.UNION 会隐式地做一个 DISTINCT,所以test"只出现一次.

                The word "test" occurs in both lists. UNION will do a DISTINCT implicitly, so "test" appears only once.

                SELECT * 
                FROM (VALUES('this'),('is'),('a'),('test')) AS tbl(Words)
                UNION
                SELECT * 
                FROM (VALUES('and'),('another'),('test')) AS tbl(Words);
                

                UNION ALL一样会让test"出现两次

                The same with UNION ALL will let the "test" appear twice

                SELECT * 
                FROM (VALUES('this'),('is'),('a'),('test')) AS tbl(Words)
                UNION ALL
                SELECT * 
                FROM (VALUES('and'),('another'),('test')) AS tbl(Words);
                

                您可以将 UNION SELECT 放入周围的 SELECT(UNIONUNION ALL 并设置FOR XML PATH 用于整个结果集

                You can put your UNION SELECT into a surrounding SELECT (either UNION or UNION ALL and set the FOR XML PATH for the whole result-set

                命名空间重复创建,没有错,但是很烦(见这个:https://stackoverflow.com/a/35648751/5089204 和链接的连接文章)

                The namespace is created repeatedly, not wrong, but annoying (see this: https://stackoverflow.com/a/35648751/5089204 and the linked Connect-Article)

                WITH XMLNAMESPACES(DEFAULT 'Dummy') 
                SELECT *
                FROM
                (
                    SELECT * 
                    FROM (VALUES('this'),('is'),('a'),('test')) AS tbl(Words)
                    UNION
                    SELECT * 
                    FROM (VALUES('and'),('another'),('test')) AS tbl(Words)
                ) AS MetaTable
                FOR XML Path(''),ROOT('UNION_TEST');
                

                这将带回两个列表,每个列表都在它自己的 XML 标签中,还有重复的命名空间(见之前)

                This will bring back both lists, each in its own XML tag, also repeated namespace (see before)

                WITH XMLNAMESPACES(DEFAULT 'Dummy') 
                SELECT
                 (
                    SELECT * 
                    FROM (VALUES('this'),('is'),('a'),('test')) AS tbl(Words)
                    FOR XML PATH(''),ROOT('FirstBlock'),TYPE
                 )
                ,(
                    SELECT * 
                    FROM (VALUES('and'),('another'),('test')) AS tbl(Words)
                    FOR XML PATH(''),ROOT('FirstBlock'),TYPE
                 )
                FOR XML Path(''),ROOT('UNION_TEST');
                

                最后你也可以使用它(无论是否使用ALL):

                And finally you can use this too (either with ALL or not):

                WITH XMLNAMESPACES(DEFAULT 'Dummy') 
                SELECT * 
                FROM (VALUES('this'),('is'),('a'),('test')) AS tbl(Words)
                UNION ALL
                SELECT * 
                FROM (VALUES('and'),('another'),('test')) AS tbl(Words)
                FOR XML PATH(''),ROOT('UNION_TEST');
                

                这篇关于创建 XML 文件时在 SQL 中联合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:对 XML 中的节点值求和时 SQL Server 的奇怪行为 下一篇:SQL 中 XML 粉碎的替代方案

                相关文章

                最新文章

              2. <legend id='ytugc'><style id='ytugc'><dir id='ytugc'><q id='ytugc'></q></dir></style></legend>

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

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