Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3601644
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:38:50+00:00 2026-05-18T20:38:50+00:00

Here are my tables: —————————————– Hotels —————————————– HotelID | HotelName —————————————– —————————————– HotelImages —————————————–

  • 0

Here are my tables:

-----------------------------------------
Hotels
-----------------------------------------
HotelID | HotelName
-----------------------------------------


-----------------------------------------
HotelImages
-----------------------------------------
HotelImageID | HotelID | Filename
-----------------------------------------

I’d like to create a SELECT statement that will return the data like so:

HotelID | HotelName | Images
-----------------------------------------
1 | Hotel1 | Image1,Image2,Image3
2 | Hotel2 | Image4,Image5,Image6

How can I modify my query to do this? I have:

SELECT H.HotelID, H.HotelName, '' AS Images
FROM Hotels H

I know I can use COALESCE to create a comma-delimited list:

DECLARE @Images varchar(500)
SELECT @Images = COALESCE(@Images + ',', '') + CAST(Filename AS varchar(100)) FROM HotelImages WHERE HotelID = 1
SELECT @Images

But I don’t know how to integrate that into my current query so that the list can be returned with the rest of the hotel data.

I should mention I’m using SQL Server 2000.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:38:51+00:00Added an answer on May 18, 2026 at 8:38 pm

    Since this is SQL 2000 you options are somewhat limited. You can’t use FOR XML PATH or recursive CTEs

    Also the COALESCE trick only works for stuffing a comma delimited list for single hotel. Not all of them.

    Honestly you’re better off doing this in the client. But if that can’t be done (certain Report Apps that-which-must-not-be-named come to mind) you can do the following.

    Note the number of times the loop process is equal to maximum number of files associated with a hotel. This is much better than setting up a loop for each hotel (e.g. calling a UDF from the select clause).

    DECLARE @foo table ( HotelID int , lastID int , FileNameList varchar(8000))
    
    INSERT INTO @Foo 
    
    SELECT
        start.HotelID,
        start.firstID ,
        id.FileName 
    
    FROM
        (SELECT 
            HotelID,
            min(HotelImageID ) firstID
        from 
            HotelImages
    
    
        group by HotelID) start
        INNER JOIN HotelImages hi
        ON start.firstID = hi.HotelImageID 
    
    
    WHILE @@RowCount <> 0 
    BEGIN
    
        UPDATE @foo
        SET 
            FileNameList = FileNameList + ',' + FileName
            lastID  = Xnext.nextID
        FROM 
            @foo f
            INNER JOIN HotelImages hi
            ON f.HotelID = id.HotelID
            INNER JOIN 
            (SELECT 
    
                id.HotelID,
                min(HotelImageID ) nextID
            from HotelImages hi
                  inner join @foo f
                  on f.HotelID = id.HotelID
            where id.HotelImageID  > f.lastid
    
    
            group by id.HotelID) Xnext
    
            ON Xnext.nextID = id.HotelImageID 
    
    
    
    END 
    
    SELECT 
        h.HotelID 
        h.HotelName,
        f.FileNameList
    FROM
        Hotels h
        INNER JOIN @foo f
        ON h.hotelId = f.HotelId
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

here are the 2 tables i have, i want to implement an trigger that
I have two tables here - one is in UTF and holds Arabic text
SQL newbie here :) Here are my tables if anyone's interested. AHH, cant post
here is how my tables are currently setup: Dataset | - Dataset_Id - Int
I'm trying to join tables to get a count on a field but am
I'm implementing a view on a model with three tables, one of them join
I am trying to get a list of product id's that do not have
I would like to show all the usernames who have interacted with a bug
have a poll table which has 10 categories and I want to view the
I want to use utf 8 right now , but all my data is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.