17

我从我的 SQL SERVER 数据库中的各种表中生成了以下 XML

<XMLData>
...
<Type>1</Type>
...
</XMLData>

<XMLData>
...
<Type>2</Type>
...
</XMLData>

<XMLData>
...
<Type>3</Type>
...
</XMLData>

我需要的最终输出是单一组合如下:

<AllMyData>
    <XMLData>
        ...
        <Type>1</Type>
        ...
    </XMLData>
    <XMLData>
        ...
        <Type>2</Type>
        ...
    </XMLData>
    <XMLData>
        ...
        <Type>3</Type>
        ...
    </XMLData>
<AllMyData>

注意 - 我组合的所有独立元素都具有相同的标签名称。

提前感谢您查找此内容。

4

7 回答 7

19

我从我的 SQL SERVER 数据库中的各种表中生成了以下 XML

取决于你如何拥有它,但如果它在 XML 变量中,你可以这样做。

declare @XML1 xml
declare @XML2 xml
declare @XML3 xml

set @XML1 = '<XMLData><Type>1</Type></XMLData>'
set @XML2 = '<XMLData><Type>2</Type></XMLData>'
set @XML3 = '<XMLData><Type>3</Type></XMLData>'

select @XML1, @XML2, @XML3 
for xml path('AllMyData')
于 2012-07-28T09:58:33.143 回答
6

我不能发表评论,但可以回答,所以即使我认为评论更合适,我将扩展上面的 Rainabba 回答以增加更多控制。我的 .Net 代码需要知道返回的列名,所以我不能依赖自动生成的名称,但需要上面提供的非常提示 Rainabba。

这样,xml 可以有效地连接成单行并命名结果列。您可以使用同样的方法将结果分配给 XML 变量,并从 PROC 中返回。

SELECT (
 SELECT XmlData as [*]
 FROM
     (
     SELECT
         xmlResult AS [*]
     FROM
         @XmlRes
     WHERE
         xmlResult IS NOT NULL
     FOR XML PATH(''), TYPE
     ) as DATA(XmlData)
 FOR XML PATH('')
) as [someColumnName]
于 2013-10-01T00:47:03.380 回答
5

如果使用for xml type,则可以组合 XML 列而不强制转换它们。例如:

select  *
from    (
        select  (
                select  1 as Type
                for xml path(''), type
                )
        union all
        select  (
                select  2 as Type
                for xml path(''), type
                )
        union all
        select  (
                select  3 as Type
                for xml path(''), type
                )
        ) as Data(XmlData)
for xml path(''), root('AllMyData'), type

这打印:

<AllMyData>
    <XmlData>
        <Type>1</Type>
    </XmlData>
    <XmlData>
        <Type>2</Type>
    </XmlData>
    <XmlData>
        <Type>3</Type>
    </XmlData>
</AllMyData>
于 2012-07-28T09:49:34.013 回答
3

作为 Mikael Eriksson 答案的附录 - 如果您有一个需要不断添加节点然后想要将其分组到单个节点下的过程,这是一种方法:

declare @XML1 XML
declare @XML2 XML
declare @XML3 XML
declare @XMLSummary XML

set @XML1 = '<XMLData><Type>1</Type></XMLData>'
set @XMLSummary = (SELECT @XMLSummary, @XML1 FOR XML PATH(''))

set @XML2 = '<XMLData><Type>2</Type></XMLData>'
set @XMLSummary = (SELECT @XMLSummary, @XML2 FOR XML PATH(''))

set @XML3 = '<XMLData><Type>3</Type></XMLData>'
set @XMLSummary = (SELECT @XMLSummary, @XML3 FOR XML PATH(''))


SELECT @XMLSummary FOR XML PATH('AllMyData')
于 2013-02-26T16:03:54.443 回答
1

我需要做同样的事情,但不知道有多少行/变量涉及并且没有添加额外的模式,所以这是我的解决方案。按照这种模式,我可以根据需要生成任意数量的片段,将它们组合起来,在 PROCS 之间传递它们,甚至从 procs 中返回它们,并且在任何时候,将它们包装在容器中,而无需修改数据或被迫将 XML 结构添加到我的数据。我将这种方法与 HTTP 端点一起使用以提供 XML Web 服务,并使用另一种将 XML 转换为 JSON 的技巧来提供 JSON WebServices。

    -- SETUP A type (or use this design for a Table Variable) to temporarily store snippets into. The pattern can be repeated to pass/store snippets to build
    -- larger elements and those can be further combined following the pattern.
    CREATE TYPE [dbo].[XMLRes] AS TABLE(
        [xmlResult] [xml] NULL
    )
    GO


    -- Call the following as much as you like to build up all the elements you want included in the larger element
    INSERT INTO @XMLRes ( xmlResult )
        SELECT
            (    
                SELECT
                    'foo' '@bar'
                FOR XML
                    PATH('SomeTopLevelElement')
            )

    -- This is the key to "concatenating" many snippets into a larger element. At the end of this, add " ,ROOT('DocumentRoot') " to wrapp them up in another element even
    -- The outer select is a time from user2503764 that controls the output column name

   SELECT (
    SELECT XmlData as [*]
    FROM
        (
        SELECT
            xmlResult AS [*]
        FROM
            @XmlRes
        WHERE
            xmlResult IS NOT NULL
        FOR XML PATH(''), TYPE
        ) as DATA(XmlData)
    FOR XML PATH('')
   ) as [someColumnName]
于 2013-10-01T00:17:44.843 回答
0
ALTER PROCEDURE usp_fillHDDT @Code  int

AS
BEGIN

 DECLARE @HD XML,@DT XML;  

    SET NOCOUNT ON;
    select invhdcode, invInvoiceNO,invDate,invCusCode,InvAmount into #HD
    from dbo.trnInvoiceHD where invhdcode=@Code

    select invdtSlNo No,invdtitemcode ItemCode,invdtitemcode ItemName,
    invDtRate Rate,invDtQty Qty,invDtAmount Amount ,'Kg' Unit into #DT from
     dbo.trnInvoiceDt  where invDtTrncode=@Code 

    set @HD = (select * from #HD HD  FOR XML AUTO,ELEMENTS XSINIL);
    set @DT = (select* from #DT DT FOR XML AUTO,ELEMENTS XSINIL);

    SELECT CAST ('<OUTPUT>'+ CAST (ISNULL(@HD,'') AS VARCHAR(MAX))+ CAST ( ISNULL(@DT,'') AS VARCHAR(MAX))+ '</OUTPUT>'   AS XML)

END
于 2015-03-23T21:04:50.943 回答
-1
public String ReplaceSpecialChar(String inStr)
{
    inStr = inStr.Replace("&", "&amp;");
    inStr = inStr.Replace("<", "&lt;");
    inStr = inStr.Replace(">", "&gt;");
    inStr = inStr.Replace("'", "&#39;");
    inStr = inStr.Replace("\"", "&quot;");
    return inStr;
}
于 2015-03-25T04:04:50.177 回答