Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 COALESCE Question

Author  Topic 

BigRetina
Posting Yak Master

144 Posts

Posted - 2003-05-04 : 03:21:51
Salute..
I need to view each employee in Employees table with his children from EmployeeFamily table like :

EmployeeId Children
---------- ----------
1 John,Mia,Karla
2 Sofi
3 Peter,Sandy
...etc...
Can I do this in ONE statement???

I looked into the COALESCE use in this forum i ended up with the following :
DECLARE @ChildrenList varchar(100)
SELECT @ChildrenList = COALESCE(@ChildrenList + ', ', '') + CAST(ChildName AS varchar(5))
FROM EmployeeFamily
WHERE EmployeeId IN (Select EmployeeId from Employees)
SELECT @ChildrenList AS Children

BUT...this solution shows ALL children for all employees in ONE COMMA SEPERATED STRING!..NOT WHAT I WANT!..
Any ideas??
Thanks IN Advance

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2003-05-04 : 08:49:12
Check out this article:

http://www.sqlteam.com/item.asp?ItemID=11021

then read the comments -- posted by Byrmol is a function that will do exactly what you want. I recommend the function if you have Sql 2000, it works very well, and is easy to use.

- Jeff
Go to Top of Page
   

- Advertisement -