Teste Converter Linhas para String

0

Please login or register to vote for this query.

(click on this box to dismiss)

Meta Stack Overflow

Q&A about the site for professional and enthusiast programmers

CREATE table #Empresa
    ([Id] int, [EmpresaNome] varchar(30))
;
CREATE table #Contato
    ([Id] int, [IdEmpresa] int, [ContatoNome] varchar(30))
;
    
INSERT INTO #Empresa
    ([Id], [EmpresaNome])
VALUES
    (1, 'Empresa1'),
    (2, 'Empresa2'),
    (3, 'Empresa3')
;

INSERT INTO #Contato
    ([Id], [IdEmpresa],[ContatoNome])
VALUES
    (1,1, 'Contato1emp1'),
    (2,1, 'Contato2emp1'),
    (3,2, 'Contato3emp2'),
    (4,2, 'Contato4emp2')
;

select e.*,
( 
  SELECT ',' + ContatoNome AS 'data()' 
         FROM #Contato where idEmpresa = e.Id FOR XML PATH('')
) as contatos
from #Empresa as e

Enter Parameters

Options:
Switch to main site
loading Hold tight while we fetch your results