Vra

Ek het 'n tafel met 'n struktuur soos die volgende:

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

'n ander gestoor proses te slaag, ek moet die XML om te lyk soos hierdie:

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

Die beste wat ek kan so ver doen nie is om dit so:

<root clientID="10705"/>

Ek gebruik hierdie SQL-stelling:

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

Tot dusver Ek het gekyk na die dokumentasie op die MSDN bladsy , maar ek het nie uit te kom met die gewenste resultate.


@KG,

Die uwe het vir my hierdie uitset eintlik:

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

Ek gaan om vas te hou met die FOR XML EXPLICIT van Chris Leon vir nou.

Was dit nuttig?

Oplossing

probeer

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

Ander wenke

Probeer hierdie, Chris:

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

vreeslik jammer! Ek deurmekaar wat jy vra vir. Ek verkies om die XML outomatiese net vir gemak van instandhouding, maar ek glo nie een is doeltreffend. Ek vra om verskoning vir die toesig; -)

Die gebruik van SQL Server 2005 (of vermoedelik 2008) Ek vind vir XML pad na toelaat vir baie makliker om SQL in stand te hou as vir XML Explicit (veral wanneer die SQL is langer).

In hierdie geval:

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

Ek het dit met:

select
1 as tag,
null as parent,
AccountNumber as 'root!1!clientID!element'
from
Location.LocationMDAccount
where
locationid = 'long-guid-here'
for xml explicit
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')
scroll top