patricka_0377
asked on
Looping without using cursors... How can I do the following...
I have the following two tables:
TABLE_A
ID |FIELD1|FIELD2
==================
1 | A | B
2 | D | E
3 | F | G
TABLE_B
ID| TABLE_A_ID | FIELD1
========================== =
1 | 1 | H
2 | 1 | P
3 | 1 | A
3 | 2 | P
Needs to produce the following output (without using cursors)... Is this possible?
| A | B | H,P,A |
| D | B | P |
| F | G | |
TABLE_A
ID |FIELD1|FIELD2
==================
1 | A | B
2 | D | E
3 | F | G
TABLE_B
ID| TABLE_A_ID | FIELD1
==========================
1 | 1 | H
2 | 1 | P
3 | 1 | A
3 | 2 | P
Needs to produce the following output (without using cursors)... Is this possible?
| A | B | H,P,A |
| D | B | P |
| F | G | |
which version of sql server?
ASKER
listed in the tags at the bottom of the question.
here you go
select t1.field1, t1.field2, replace((select field1 as [data()] from tableb t2 where t2.tablea_id = t1.id for xml path('')),' ',',') as field3
from tablea t1
order by t1.id
select t1.field1, t1.field2, replace((select field1 as [data()] from tableb t2 where t2.tablea_id = t1.id for xml path('')),' ',',') as field3
from tablea t1
order by t1.id
ASKER
Does not work.
Line1: Incorrect syntax near 'xml'
Line1: Incorrect syntax near 'xml'
did you mean:
| D | E | P |?
|TABLE_AFIELD1|TABLE_aFIEL D2 - TABLE_B
==================
| A | B H,P,A
| D | E P
| F | G
| D | E | P |?
|TABLE_AFIELD1|TABLE_aFIEL
==================
| A | B H,P,A
| D | E P
| F | G
ASKER
yes
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
--or :
select t1.FIELD1,t1.FIELD2, isnull( dbo.udf_FIELD1(t2.TABLE_A_ ID), '' )
from TABLE_A t1
left join
TABLE_b t2
on
t1.ID=t2.TABLE_A_ID
group by t1.FIELD1, t1.FIELD2 ,t2.TABLE_A_ID,dbo.udf_FIE LD1(t2.TAB LE_A_ID)
select t1.FIELD1,t1.FIELD2, isnull( dbo.udf_FIELD1(t2.TABLE_A_
from TABLE_A t1
left join
TABLE_b t2
on
t1.ID=t2.TABLE_A_ID
group by t1.FIELD1, t1.FIELD2 ,t2.TABLE_A_ID,dbo.udf_FIE
ASKER
http://sqlblog.com/blogs/adam_machanic/archive/2006/07/12/rowset-string-concatenation-which-method-is-best.aspx
found this to be the best answer
found this to be the best answer
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.