显式的SQL Server 2005 - 需要帮助格式

我有一个如下结构的表格:

------------------------------
LocationID     | AccountNumber
------------------------------
long-guid-here | 12345
long-guid-here | 54321

要传入另一个存储过程,我需要XML如下所示:

 
    12345
    54321

我迄今为止所能做到的最好的事情就是像这样得到它:


我正在使用这个SQL语句:

SELECT
    1 as tag,
    null as parent,
    AccountNumber as 'root!1!clientID'
FROM
    Location.LocationMDAccount
WHERE
    locationid = 'long-guid-here'
FOR xml EXPLICIT

到目前为止,我已经查看了 MSDN页面上的文档,但我还没有达到预期的结果。


@公斤,

你实际上给了我这个输出:


  
    10705
  

我现在要坚持使用Chris Leon的 FOR xml EXPLICIT

0
额外 编辑
意见: 1

5 答案

我明白了:

select
1 as tag,
null as parent,
AccountNumber as 'root!1!clientID!element'
from
Location.LocationMDAccount
where
locationid = 'long-guid-here'
for xml explicit
0
额外

试试这个,克里斯:

SELECT
    AccountNumber as [clientId]
FROM
    Location.Location root
WHERE
    LocationId = 'long-guid-here'
FOR
    xml AUTO, ELEMENTS

非常抱歉!我混淆了你所要求的。我更喜欢XML AUTO以便维护,但我相信任何一个都是有效的。我对这个疏忽表示歉意;-)

0
额外
SELECT 1             as tag,
       null          as parent,
       AccountNumber as 'clientID!1!!element'
FROM Location.LocationMDAccount
WHERE locationid = 'long-guid-here'
FOR xml EXPLICIT, root('root')
0
额外

尝试

SELECT
    1 AS Tag,
    0 AS Parent,
    AccountNumber AS [Root!1!AccountNumber!element]
FROM
    Location.LocationMDAccount
WHERE
    LocationID = 'long-guid-here'
FOR xml EXPLICIT
0
额外

使用SQL Server 2005(或者大概是2008),我发现对于XML PATH来说,为了更容易维护SQL而不是XML Explicit(特别是一旦SQL更长)。

在这种情况下:

SELECT AccountNumber as "clientID"
FROM Location.LocationMDAccount
WHERE locationid = 'long-guid-here'
FOR xml PATH (''), Root ('root');
0
额外