Link to home
Start Free TrialLog in
Avatar of patricka_0377
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 |       |
Avatar of momi_sabag
momi_sabag
Flag of United States of America image

which version of sql server?
Avatar of patricka_0377
patricka_0377

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
Does not work.

Line1: Incorrect syntax near 'xml'
Avatar of EugeneZ
did you mean:
 | D | E | P |?
 
|TABLE_AFIELD1|TABLE_aFIELD2  -  TABLE_B
==================
| A | B                                                              H,P,A
| D | E                                                               P
| F | G
yes
ASKER CERTIFIED SOLUTION
Avatar of EugeneZ
EugeneZ
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
--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_FIELD1(t2.TABLE_A_ID)  
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial