Solved

Return all rows from child table as a comma diliminated row in parent table

Posted on 2011-03-19
5
368 Views
Last Modified: 2012-08-13
I have a table called Projects and for every row in Projects, there are one or more rows (Called Site) in a related table. In my stored procedure I would like to return the Project and all the sites in a comma diliminated column

So the returned row would look like this:

ProjectName,
(Site1,Site2,Site2) as Sites

Thanks for any help.
0
Comment
Question by:AkAlan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 250 total points
ID: 35172638
0
 
LVL 4

Expert Comment

by:LeDaouk
ID: 35172645
you have to make a function that you pass for it the ID of the project and return the string you want
0
 
LVL 32

Assisted Solution

by:Ephraim Wangoya
Ephraim Wangoya earned 250 total points
ID: 35172646

Here
SELECT ProjectID, Stuff(
                  (Select  ',' + Site
                              From    ChildTable
                              Where   ChildTable.ProjectID = Projects.ProjectID
                              For xml Path('')
                  )
                        
                  , 1, 1, '') as [Sites]
                  
                        
From Projects

Open in new window

0
 
LVL 4

Expert Comment

by:LeDaouk
ID: 35172676
try to Create this function



USE [... Database Name ...]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

Create FUNCTION [dbo].[Fetch_Sites]
(
      @ProjectID as numeric
)
RETURNS nvarchar(1000)
AS
BEGIN

      


      
      
      DECLARE  @Result as nvarchar(1000)
      DECLARE Sites CURSOR FOR
      
      SELECT     Site FROm Sites Where Project_ID = @ProjectID
      SET @Result = ''
      SET @Name = ''
      OPEN Sites
      
            WHILE @@Fetch_Status = 0
      
            BEGIN

                  SET @Result = @Result + @Name + ','      
                  FETCH Companion INTO @Name
                       
                  
            END
            if len(@Result)>1
                  begin
                        SET @Result = left(@Result ,len(@Result)-1)
                        SET @Result = Right(@Result ,len(@Result)-1)      
                  end      
            else
                  SET @Result=''

      CLOSE Sites
      

      
      
      RETURN  @Result


END


then you can do
Sletec ProjectName,Fetch_Sites(Project_ID) as Site
From Projects
0
 
LVL 6

Author Comment

by:AkAlan
ID: 35173523
OK, I used the STUFF solution which  EMoreau and ewangoya both suggested. I like the idea of being able to see what is going on right in the sproc, rather than have to go look at the function to see what is going on there. I am going to post my solution because I needed to include a join and I am passing a parameter to the sproc. Thanks guys, I had never heard of the STUFF Function pretty cool, I will be reusing it a lot.

STUFF((SELECT ',' + s.SiteName FROM  ENG_OaSites    oas
			  JOIN
			  ALL_Sites	     s	ON oas.SiteID = s.SiteID 
                                                  WHERE oas.OaNumber = @OA_Nr 
                                                  ORDER BY s.SiteName FOR XML PATH ('')
                                                 )
			,1,1, '') As OaSites,

Open in new window

0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question