gpt4 book ai didi

sql-server - SQL UNION FOR XML 名称输出列

转载 作者:行者123 更新时间:2023-12-02 07:26:20 32 4
gpt4 key购买 nike

我正在尝试从 SQL 生成 XML 输出,需要使用 UNION 语句并命名输出列。

当我不需要使用 UNION 语句时,我之前就使用了以下方法:

select(
SELECT

[CompanyName],
[Address1],
[Address2],
[Address3],
[Town],
[County],
[Postcode],
[Tel],
[Fax],
[Email],
[LocMap]

FROM [UserAccs] FOR XML PATH ('AccountDetails'), root ('Root')
) as XmlOutput

将输出 XML 列命名为 XmlOutput

我现在正在尝试:

select(
SELECT

[CompanyName],
[Address1],
[Address2],
[Address3],
[Town],
[County],
[Postcode],
[Tel],
[Fax],
[Email],
[LocMap]

FROM [UserAccs]

UNION

SELECT

[CompanyName],
[Address1],
[Address2],
[Address3],
[Town],
[County],
[Postcode],
[Tel],
[Fax],
[Email],
[LocMap]

FROM [UserAppAccs]



FOR XML PATH ('AccountDetails'), root ('Root')
) as XmlOutput

但是收到错误消息,有人知道解决这个问题的方法吗?

The FOR XML clause is invalid in views, inline functions, derived tables, and subqueries when they contain a set operator. To work around, wrap the SELECT containing a set operator using derived table syntax and apply FOR XML on top of it.

谢谢J.

最佳答案

将 2 个选择包裹在一个选择上,如下所示:

select (
select id, name from (
select id, name
from xmltest
UNION
select id, name
from xmltest
) A
FOR XML PATH ('AccountDetails'), root ('Root')
) As XmlOutput

关于sql-server - SQL UNION FOR XML 名称输出列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9311198/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com