2008-08-05 61 views
8

我有一個類似如下結構的表:SQL Server 2005中對於XML明確 - 需要幫助的格式

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

要傳遞到另一個存儲過程,我需要的XML看起來像這樣:

<root> 
    <clientID>12345</clientID> 
    <clientID>54321</clientID> 
</root> 

我已經能夠做到到目前爲止是越來越像這種情況的最好:

<root clientID="10705"/> 

我使用這個SQL小號tatement:

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

到目前爲止,我已經看了看文檔上the MSDN page,但我不希望的結果出來。


@KG,

此致給了我這個輸出實際上是:

<root> 
    <Location.LocationMDAccount> 
    <clientId>10705</clientId> 
    </Location.LocationMDAccount> 
</root> 

我會與​​從克里斯·萊昂堅持現在。

回答

1

嘗試

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

試試這個,克里斯:

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 'root!1!clientID!element' 
from 
Location.LocationMDAccount 
where 
locationid = 'long-guid-here' 
for xml explicit 
0

使用SQL Server 2005(或2008年大概),我找到XML PATH允許更容易維護SQL比XML明確(特別是一旦SQL更長)。

在這種情況下:

SELECT AccountNumber as "clientID" 
FROM Location.LocationMDAccount 
WHERE locationid = 'long-guid-here' 
FOR XML PATH (''), Root ('root'); 
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')