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
 SQL Server Development (2000)
 Creating a comma-delimited list in SQL

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2006-02-06 : 11:33:59
Rick writes "Mr. Guru,

I am trying to display a list based on 3 tables, which are similar to those Barry had presented in
"Advice Needed on a Funky Data Model".


Let's just say the 3 tables were exactly the same as Barrys;

Car - CarID, CarName, etc,
Category - CategoryID, CategoryName, etc,
(e.g., Mini-van, sports, SUV, luxury, etc.)
Car_Category - CarID, CategoryID

Now, the list I'm trying to extract should look something like this;


CarID CarName Categories
-------------------------------------------
1 FordGT sports, luxury
2 Mustang sports
3 EscapeHybrid SUV, hybird
4 Expedition SUV, luxury


I assume this is more of a FAQ item since there should be a lot of similar situations
(e.g., Users - Roles - User_Roles, Product - Parts - Product_Parts, etc) but I couldn't find
anything on the web.

Thanks,
Rick"

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2006-02-06 : 13:40:07
first create a function to return a csv for a given car then use the function in a select statement of all cars:


set nocount on
use pubs
go

create table car (carid int identity(1,1) primary key, carname varchar(30))
create table Category (Categoryid int identity(1,1) primary key, CategoryName varchar(30))
go
create table Car_Category (CarID int references car(carid), CategoryID int references category(categoryid))
go

insert car (carname)
select 'FordGT' union all
select 'Mustang' union all
select 'EscapeHybrid'

insert Category (categoryName)
select 'sports' union all
select 'luxury' union all
select 'SUV' union all
select 'hybrid'
go

insert Car_Category (carid, categoryid)
select 1,1 union all
select 1,2 union all
select 2,1 union all
select 2,3 union all
select 3,4
go

--first create a function to return a csv for a given car:
if object_id('dbo.CategoriesByCarID') > 0
drop function dbo.CategoriesByCarID
go

create function dbo.CategoriesByCarID(@carid int)
returns varchar(2000)
as
begin
declare @cats varchar(2000)

select @cats = coalesce(@cats + ', ' + c.CategoryName, c.CategoryName)
from Car_Category cc
join Category c
on c.categoryid = cc.categoryid
where cc.carid = @carid
order by c.categoryName

return @cats
end
go

--now use the function
select c.CarID
,c.CarName
,Categories = dbo.CategoriesByCarID(c.carid)
from dbo.Car c

drop table Car_Category
go
drop table Car
drop table category


Be One with the Optimizer
TG
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-02-07 : 10:10:42
Also read this why Function is needed
http://sqljunkies.com/WebLog/amachanic/archive/2004/11/10/5065.aspx?Pending=true

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -