SQL Server 2005 wählen Sie für XML-Pfad mit Union in Unterauswahlproblem
-
20-09-2019 - |
Frage
Ich bin eher mit SQL Server erfahren „für XML-Pfad wählen Sie“ Abfragen, aber jetzt ich laufe in ein seltsames Problem.
Die folgende Abfrage funktioniert:
select
(
select
'Keyfield1' as "@Name",
t1.Keyfield1 as "Value"
from MyTable t1
where
t1.KeyField1= t2.KeyField1 and
t1.KeyField2= t2.KeyField2
for xml path('Field'),type, elements
) as 'Key'
from MyTable t2
for XML path('Path') , elements XSINIL, root('Root')
Dies wird zur Folge haben (für eine Dummy-Datensatz) in diesem XML:
<Root>
<Path>
<Key Name="KeyField1">
<Value>DummyValue1</Value>
</Key>
</Path>
</Root>
In meinem Ergebnis dieses (Teil einer größeren) Aussage muß ich die 2. keyfield auch:
<Root>
<Path>
<Key Name="KeyField1">
<Value>DummyValue1</Value>
</Key>
<Key Name="KeyField2">
<Value>DummyValue2</Value>
</Key>
</Path>
</Root>
Also ich meine (Teil-) Abfrage mit einer veränderten gewerkschafts wählen:
select
(
select
'Keyfield1' as "@Name",
t1.Keyfield1 as "Value"
union all
select
'Keyfield2' as "@Name",
t1.Keyfield2 as "Value"
from MyTable t1
where
t1.KeyField1= t2.KeyField1 and
t1.KeyField2= t2.KeyField2
for xml path('Field'),type, elements
) as 'Key'
from MyTable t2
for XML path('Path') , elements XSINIL, root('Root')
Aber ich bekomme jetzt den Fehler „Nur einen Ausdruck in der Auswahlliste angegeben werden kann, wenn die Unterabfrage nicht mit EXISTS eingeführt wird.“
mehrere Datensätze in einer Unterabfrage, um mit XML-Pfad Hexe Ergebnisse in mehrere ElementeIch weiß, es ist möglich. Aber ich verstehe nicht, warum dies nicht mit einer Union durchgeführt werden.
Kann jemand mich in der richtigen Richtung, wie der XML accomplisch mit dem 2 KeyFields in meiner (sub) Abfrage?
Thanx Sie sehr.
Lösung
Das Problem mit Ihrem subselect ist, dass der erste Teil bezieht sich nicht auf eine Tabelle überhaupt (ohne FROM-
clause).
Diese Auflistung gibt mir die Ausgabe, die Sie angefordert:
declare @mytable table (
keyfield1 nvarchar(20),
keyfield2 nvarchar(20)
)
insert into @mytable values ('Dummyvalue1', 'Dummyvalue2')
select * from @mytable
select
(
select
'Keyfield1' as "@Name",
t1.Keyfield1 as "Value"
from @mytable t1
where
t1.KeyField1= t2.KeyField1 and
t1.KeyField2= t2.KeyField2
for xml path('Field'),type, elements
) as 'Key'
from @mytable t2
for XML path('Path') , elements XSINIL, root('Root')
select
(
select * from (
select
'Keyfield1' as "@Name",
t1.Keyfield1 as "Value"
from @MyTable t1
where
t1.KeyField1= t2.KeyField1
union all
select
'Keyfield2' as "@Name",
t3.Keyfield2 as "Value"
from @MyTable t3
where
t3.KeyField2= t2.KeyField2) a
for xml path('Field'),type, elements
) as 'Key'
from @MyTable t2
for XML path('Path') , elements XSINIL, root('Root')
Andere Tipps
Hier ist ein vereinfachtes Beispiel, aber nicht das Sie bekommen, was Sie brauchen?
select
(
select
'Keyfield1' as "@Name",
'Blah' as "Value"
for xml path('Key'),type, elements
),
(
select
'Keyfield2' as "@Name",
'Blah' as "Value"
for xml path('Key'),type, elements
)
for XML path('Path') , elements XSINIL, root('Root')