SQL Server 2005 selezionare per il percorso XML con l'unione a problemi sotto-selezione
-
20-09-2019 - |
Domanda
Sono piuttosto sperimentato con il server SQL "selezionare per il percorso XML" query, ma ora mi imbatto in uno strano problema.
La query seguente funziona bene:
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')
Questo sarà il risultato (per un set di dati fittizio) in questo XML:
<Root>
<Path>
<Key Name="KeyField1">
<Value>DummyValue1</Value>
</Key>
</Path>
</Root>
Nel mio risultato di questa (parte di una più grande) dichiarazione ho bisogno del 2 ° keyfield troppo:
<Root>
<Path>
<Key Name="KeyField1">
<Value>DummyValue1</Value>
</Key>
<Key Name="KeyField2">
<Value>DummyValue2</Value>
</Key>
</Path>
</Root>
Così ho cambiato la mia (sub) query con un sindacato di selezione per:
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')
Ma ora ottengo l'errore "Solo un'espressione può essere specificato nella lista di selezione quando il subquery non è introdotta con esiste".
Lo so che è possibile avere più record in una sottoquery con FOR XML risultati percorso strega in più elementi. Ma io non capisco perché questo non può essere fatto con un sindacato.
Può qualcuno mi ha messo nella giusta direzione come accomplisch XML con i 2 keyfields nel mio (sub) ricerca?
Grazie mille.
Soluzione
Il problema con il vostro subselect è che la prima parte non si riferisce a qualsiasi tavolo a tutti (senza FROM-
clause).
Questo elenco mi dà l'output richiesto:
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')
Altri suggerimenti
Ecco un esempio semplificato, ma fa questo si ottiene quello che ti serve?
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')