sql server - How to combine multiple rows from three tables into one single string in SQL? -


i have 3 tables store different kinds of data according admission process.

the first table ([dbo].[inscripciones_pregrado]) stores basic contact info of future student. second table ([dbo].[checklist_documentos]) stores names , id's documents student must have complete admission process. third table ([dbo].[checklist_pregrado]) stores documents student has brought admissions office.

i need combine info can export excel file holding contact info, , document's checklist each student.

so, let's table 1 haves 4 records: [dbo].[inscripciones_pregrado]

enter image description here

the second table ([dbo].[checklist_documentos]) haves 8 records, different types of requested documents: enter image description here

and third table ([dbo].[checklist_pregrado]) haves 16 records:

enter image description here

what need 3 tables combined according aspirant's id:

enter image description here

so far, i've tried different types of codes:

code 1

select     [dbo].[inscripciones_pregrado].[id],     [dbo].[inscripciones_pregrado].[name],     [dbo].[inscripciones_pregrado].[programid],     [dbo].[checklist_documentos].[document]  [dbo].[inscripciones_pregrado]   inner join     [dbo].[checklist_pregrado]     on [dbo].[checklist_pregrado].[idform] = [dbo].[inscripciones_pregrado].[id]  inner join     [dbo].[checklist_documentos]     on [dbo].[checklist_documentos].[id] = [dbo].[checklist_pregrado].[iddoc] 

this 1 gets 1 line per each document, name , other records repeated according number of documents aspirant has on folder.

code 2

select     [dbo].[inscripciones_pregrado].[id],     [dbo].[inscripciones_pregrado].[nombres],     [dbo].[inscripciones_pregrado].[apellido1],     (stuff((select cast(', ' + [dbo].[checklist_pregrado].[iddocumento] varchar(max))         [dbo].[checklist_pregrado]         where([dbo].[inscripciones_pregrado].[id] = [dbo].[checklist_pregrado].[idform])         xml path ('')), 1, 2, '')) [docs] [dbo].[inscripciones_pregrado] 

this code get's , error: conversion failed when converting varchar value ', ' data type int.

i appreciate , comments.

thanks

for code 2, can fix error doing cast before concatenation.

in other words:

(stuff((select ', ' + cast([dbo].[checklist_pregrado].[iddocumento] varchar(max)) 

Comments