我的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>
注意 - 我合并的所有独立元素都具有相同的标记名称。
提前感谢您查看此内容。
答案 0 :(得分:17)
我在SQL中的各种表中生成了以下XML SERVER数据库
取决于你如何拥有它,但如果它在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')
答案 1 :(得分: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]
答案 2 :(得分: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>
答案 3 :(得分: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')
答案 4 :(得分:1)
我需要做同样的事情,但不知道有多少行/变量,没有添加额外的模式,所以这是我的解决方案。遵循这种模式,我可以生成尽可能多的片段,组合它们,在PROCS之间传递它们甚至从procs返回它们,并且在任何时候,将它们包装在容器中,而无需修改数据或强制将XML结构添加到我的数据。我将此方法与HTTP端点一起使用以提供XML Web服务,并使用另一种将XML转换为JSON的技巧,以提供JSON Web服务。
-- 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]
答案 5 :(得分: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
答案 6 :(得分:-1)
public String ReplaceSpecialChar(String inStr)
{
inStr = inStr.Replace("&", "&");
inStr = inStr.Replace("<", "<");
inStr = inStr.Replace(">", ">");
inStr = inStr.Replace("'", "'");
inStr = inStr.Replace("\"", """);
return inStr;
}