Pregunta

Estoy bastante experimentado con el servidor SQL "para seleccionar ruta XML" consultas pero ahora me encuentro con un problema extraño.

La siguiente consulta funciona bien:

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')

Esto dará como resultado (para un conjunto de datos ficticio) en este XML:

<Root>  
  <Path>
    <Key Name="KeyField1">
      <Value>DummyValue1</Value>
    </Key>
  </Path>
</Root>

En mi resultado de esta (parte de una más grande) Declaración necesito la segunda KeyField también:

<Root>  
  <Path>
    <Key Name="KeyField1">
      <Value>DummyValue1</Value>
    </Key>
    <Key Name="KeyField2">
      <Value>DummyValue2</Value>
    </Key>
  </Path>
</Root>

Así que he cambiado de (sub) consulta con un sindicato de selección a:

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')

Pero ahora me sale el error "Sólo una expresión se puede especificar en la lista de selección cuando la subconsulta no se introduce con EXISTE."

Sé que es posible tener varios registros en una sub consulta por XML resultados de brujas ruta en varios elementos. Pero no entiendo por qué esto no se puede hacer con una unión.

Puede alguien poner en la dirección correcta de cómo accomplisch el XML con los 2 KeyFields en mi (sub) consulta?

Gracias gracias.

¿Fue útil?

Solución

El problema con la subselección es que la primera parte no se refiere a cualquier tabla en absoluto (sin FROM-clause).

Esta lista me da la salida solicitada:

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')

Otros consejos

Este es un ejemplo simplificado, pero esto se obtiene lo que necesita?

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')
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top