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 2005 Forums
 Transact-SQL (2005)
 A column that list all childs

Author  Topic 

GoDaddy
Yak Posting Veteran

64 Posts

Posted - 2009-04-16 : 13:45:24
I have 2 tables TableA, TableB (FK=TableAid)

I need to create a TableAView with a SINGLE column TableBList, that will list all the child of A seperated by a comma.

Is this possible ? It must me in a View (this is mandatory)

Thanks

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-04-16 : 13:50:15
its possible. you need to use a recursive CTE for that. see below

http://msdn.microsoft.com/en-us/library/ms186243.aspx
Go to Top of Page

GoDaddy
Yak Posting Veteran

64 Posts

Posted - 2009-04-16 : 14:11:28
ouff seems complex,

can you give a sample of the statement using recursive CTE? to build a list of values seperated by a comma
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-04-16 : 14:18:29
http://blog.mclaughlinsoftware.com/2009/04/03/t-sql-hierarchical-query/
Go to Top of Page

GoDaddy
Yak Posting Veteran

64 Posts

Posted - 2009-04-16 : 14:38:29
I'm really lost, I'm really not a dba, i'm a developer.

What do you think about creating a funtion or a SP that will use COALESCE.

My view will then call the function or SP ....
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-04-16 : 14:42:23
yup...you could create a sp if you want however i would prefer to do this inline
Go to Top of Page

GoDaddy
Yak Posting Veteran

64 Posts

Posted - 2009-04-16 : 14:50:42
Yeah me too ..... but this whole Recursive CTE ... confusing hehehe especially for a none SQL guru like me.

Appreciate for the help.
Go to Top of Page

kunal.mehta
Yak Posting Veteran

83 Posts

Posted - 2009-04-17 : 02:35:29
Hi,
this is an examle of getting comma separated values in a column. update the query as per your requirement.

Alos u need not be an SQL Guru 2 use this

declare @table1 table (date datetime,Agrid int,chkId int,name varchar(18) )



insert into @table1 values('01/01/2008',102252271,100061, 'abc')
insert into @table1 values('12/31/2008',102252271,100842, 'Eli OldName')
insert into @table1 values('11-11-2004',102252271,100843, 'Eli OldName') ;

SELECT
CAST
(
(
SELECT name + ', '
FROM @table1
FOR XML PATH ('')
)
AS VARCHAR(MAX)
)


Kunal
Go to Top of Page
   

- Advertisement -