Teste csv

0

Please login or register to vote for this query.

(click on this box to dismiss)

Stack Overflow

Q&A for professional and enthusiast programmers

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

INSERT INTO #Contato
    ([Id], [IdEmpresa],[ContatoNome],[ContatoEmail])
VALUES
    (1,1, 'Contato1emp1','email@cont1.com'),
    (2,1, 'Contato2emp1','email@cont2.com'),
    (3,2, 'Contato3emp2','email@cont3.com'),
    (4,2, 'Contato4emp2','email@cont4.com')
;

select ' NomeEmpresa,Contato1,emailcontato1,Contato2,emailcontato2' as 'ignorar' 
union all
 select e.EmpresaNome+','+
( 
  SELECT ContatoNome +','+ContatoEmail+',' AS 'data()' 
         FROM #Contato where idEmpresa = e.Id FOR XML PATH('')
         )
from #Empresa as e

Enter Parameters

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