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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Display based on reference table
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Yak Posting Veteran

54 Posts

Posted - 02/06/2013 :  22:55:19  Show Profile  Reply with Quote
Please see two master table and a link table below.
declare @tblClient table(cName varchar(30),cid int)
insert into @tblClient
select 'anu',100 union all
select 'binu',101

select * from @tblClient

declare @tblPayor table(pName varchar(30),pid int)
insert into @tblPayor
select 'Jacob',200 union all
select 'Mary',201 union all
select 'Lissy',202

select * from @tblPayor

declare @tblLink table(lid int,cid int,pid int)
insert into @tblLink
select 1,100,200 union all
select 2,100,201 union all
select 3,100,202 union all
select 4,101,200

select * from @tblLink
A cname can be assigned to multiple Pname.Based on the link table values, comma separated output requried like this.
CName PName
Anu Jacob,Mary,Lissy
Binu Jacob
Any ideas?

Very Important crosS Applying yaK Herder

52326 Posts

Posted - 02/06/2013 :  23:02:40  Show Profile  Reply with Quote

SELECT c.cName,
STUFF((SELECT ',' + pName FROM @tblPayor p 
       INNER JOIN @tblLink l
       ON =
       WHERE l.cid = c.cid
       ORDER BY l.lid
       FOR XML PATH('')),1,1,'') AS PName
FROM @tblClient c

cName	PName
anu	Jacob,Mary,Lissy
binu	Jacob

SQL Server MVP

Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.02 seconds. Powered By: Snitz Forums 2000