Pergunta

Tenho experiência bastante experiente com consultas SQL Server "Select for XML Path", mas agora encontro um problema estranho.

A consulta a seguir funciona bem:

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

Isso resultará (para um conjunto de dados dummy) neste XML:

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

No meu resultado desta declaração (parte de uma maior), também preciso do 2º Keyfield:

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

Então eu mudei minha (sub) consulta com um sindicato-selecionado para:

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

Mas agora eu recebo o erro "Apenas uma expressão pode ser especificada na lista de seleção quando a subconsulta não é introduzida."

Eu sei que é possível ter vários registros em uma subconsulta com o XML Path Witch Results em vários elementos. Mas não entendo por que isso não pode ser feito com uma união.

Alguém pode me colocar na direção certa como realizar o XML com os 2 keyfields na minha (sub) consulta?

Muito obrigado você.

Foi útil?

Solução

O problema com o seu subseleto é que a primeira parte não está se referindo a nenhuma mesa (não FROM-cláusula).

Esta listagem me dá a saída que você solicitou:

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

Outras dicas

Aqui está um exemplo simplificado, mas isso te dá o que você precisa?

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 em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top