title: I need to combine multiple rows from a subquery into a string separated by a space
body: <p>I'm creating an image gallery with albums. The gallery owner can first create one or more albums, and then create images to add to those albums. I'm having to use multiple view templates to pull this off. Regardless, here's what is hanging me up. Here is the core of my stored procedure:</p>
  
  <pre><code>DECLARE @MyString VARCHAR(MAX)
  
  SELECT @MyString = ISNULL(@MyString + ' ', ' ') + CAST(AlbumID AS VARCHAR(10)) + ' '
  FROM BD_AlbumGallery
  WHERE ImageID = PROBLEM HERE... Dont know what to do &lt;---
  ORDER BY AlbumID
  
  SELECT a.[ImageID]
        ,a.ImageFile
        ,a.[ImageTitle]
        ,a.[ImageCaption]
        ,a.[Description]
        ,a.[Active]
        ,b.[AlbumID]
        ,b.[ImageID]
        ,c.[ListingID]
        ,c.[Active]
        ,c.[LevelID]
        ,c.[Title]
  
        ,@MyString AS AlbumClass
  
  FROM BD_Gallery A
  JOIN BD_AlbumGallery B
  ON a.ImageID = b.ImageID
  JOIN BD_Listing C
  ON a.ListingID = c.ListingID
  WHERE a.ListingID = @passedListingID
    AND a.Active = 1
    AND c.Active = 1
    AND c.LevelID &gt; 5
  </code></pre>
  
  <p>So the problem is, the data is retrieved within a LI
  and one at a time of course... guess that's the obvious... to display properly in my gallery template.</p>
  
  <p>The only issue I'm having is I need the @MyString AS AlbumClass to be tied to a.ImageID, or b.ImageID, as they are the same.</p>
  
  <p>The reason is, the albums in the template are defined by a class.</p>
  
  <pre><code>&lt;li class="12 24 15 17"&gt;Rest of my code for a single image result&lt;/li&gt;
  </code></pre>
  
  <p>So the AlbumID's being utilized as class names allows me to assign them to the jQuery dropdown category/album list that's in a template right before this one. I was thinking of a SubQuery. Ultimately, I need the results (for other reasons) to display in one result set.</p>
  
  <p>Please help.. Still trying to push my way through the learning curves.</p>
  
  <p>Thanks!</p>
  
  <ul>
  <li>Patrick</li>
  </ul>