views:

35

answers:

1

Hello,

I have a SQL Server 2005 database with two tables: Order, LineItem. Each LineItem has a field called LineItemID and OrderID. I have a query that is getting all of the Order records in my database. With each Order record, I would like to retrieve a comma delimited list of LineItemIDs associated with the Order.

Is there a way to do this in SQL? I do not know how to do this.

Thank you!

+2  A: 

Here's one example, using the name column from sys.tables, of how to construct a comma-delimited string from a column:

use master
go

SELECT Stuff((SELECT ',' + name
              FROM sys.tables
              For XML PATH ('')),1,1,'')
go
Joe Stefanelli