SQL Server 2005 für XML Explicit – Benötigen Sie Hilfe bei der Formatierung?
-
08-06-2019 - |
Frage
Ich habe eine Tabelle mit einer Struktur wie der folgenden:
------------------------------
LocationID | AccountNumber
------------------------------
long-guid-here | 12345
long-guid-here | 54321
Für die Übergabe an eine andere gespeicherte Prozedur benötige ich, dass das XML so aussieht:
<root>
<clientID>12345</clientID>
<clientID>54321</clientID>
</root>
Das Beste, was ich bisher tun konnte, war, es so hinzubekommen:
<root clientID="10705"/>
Ich verwende diese SQL-Anweisung:
SELECT
1 as tag,
null as parent,
AccountNumber as 'root!1!clientID'
FROM
Location.LocationMDAccount
WHERE
locationid = 'long-guid-here'
FOR XML EXPLICIT
Bisher habe ich mir die Dokumentation angesehen die MSDN-Seite, aber ich habe nicht die gewünschten Ergebnisse erzielt.
@KG,
Deiner hat mir tatsächlich diese Ausgabe gegeben:
<root>
<Location.LocationMDAccount>
<clientId>10705</clientId>
</Location.LocationMDAccount>
</root>
Ich bleibe dabei FOR XML EXPLICIT
von Chris Leon vorerst.
Lösung
versuchen
SELECT
1 AS Tag,
0 AS Parent,
AccountNumber AS [Root!1!AccountNumber!element]
FROM
Location.LocationMDAccount
WHERE
LocationID = 'long-guid-here'
FOR XML EXPLICIT
Andere Tipps
Versuchen Sie Folgendes, Chris:
SELECT
AccountNumber as [clientId]
FROM
Location.Location root
WHERE
LocationId = 'long-guid-here'
FOR
XML AUTO, ELEMENTS
SCHRECKLICH LEID!Ich habe verwechselt, wonach Sie gefragt haben.Ich bevorzuge XML AUTO nur wegen der einfacheren Wartung, aber ich glaube, dass beides effektiv ist.Ich entschuldige mich für das Versehen ;-)
Bei Verwendung von SQL Server 2005 (oder vermutlich 2008) finde ich, dass XML PATH die Verwaltung von SQL viel einfacher ermöglicht als XML Explicit (insbesondere, wenn die SQL länger ist).
In diesem Fall:
SELECT AccountNumber as "clientID"
FROM Location.LocationMDAccount
WHERE locationid = 'long-guid-here'
FOR XML PATH (''), Root ('root');
Ich habe es bekommen mit:
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')