I have multiple tables with a foreign key from a main table. The tables are like this:
Sisters
MainID IDNO ....
111111 1 ....
111111 1 ....
111111 1 ....
222222 1 ....
111111 2 ....
Brothers
MainID IDNO ....
111111 555 ....
111111 333 ....
111111 111 ....
222222 222 ....
111111 321 ....
Uncles
MainID IDNO ....
111111 561 ....
111111 121 ....
111111 331 ....
222222 451 ....
111111 672 ....
I need to concatenate all IDNos in but I can't seem to get all as for example maybe I can't get 672
SELECT
',' + S1.IDNo + ',' + B1.IDNo + ',' + U1.IDNo AS [text()]
FROM
Sisters S1, Brothers B1, Uncles U1, MainTable
WHERE
D1.MainID = MainTable.ID
AND S1.MainID = MainTable.ID
AND B1.MainID = MainTable.ID
FOR XML PATH('')
I tried to seperate tables and concanate later but it is getting much slower that way. What can I do?
Expected output:
,1,1,1,1,2,555,333,111,222,321,561,121,331,451,672
JOINsyntax in the ANSI-92 SQL Standard (more than 20 years ago) and its use is discouragedunion all, but without sample results, that is only speculation.