473,405 Members | 2,141 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

How to return unknown number of items back in onw row

Hi

I have, hopefully, a quick question. In my database I have a table Product and a table Images.
The images has an id, product_id and image_name.
When I query for a specific product I want all the image_names associated with it in the query.... the catch.. on one line.
Now I get
123 productdesc1 image_name1
123 productdesc1 image_name2

When I want to get it like
123 productdesc1 image_name1 image_name2

The problem is that I don´t know how many images there will be

How can I solve this?

thanks
Arnar Gudnason
Oct 18 '07 #1
1 1082
Jim Doherty
897 Expert 512MB
Hi

I have, hopefully, a quick question. In my database I have a table Product and a table Images.
The images has an id, product_id and image_name.
When I query for a specific product I want all the image_names associated with it in the query.... the catch.. on one line.
Now I get
123 productdesc1 image_name1
123 productdesc1 image_name2

When I want to get it like
123 productdesc1 image_name1 image_name2

The problem is that I don´t know how many images there will be

How can I solve this?

thanks
Arnar Gudnason

Assumptions
You have a table Called Product with Fields
ProductID (datatype - int identity)
Product (datatype - text)

You have a table called Images with Fields
ID (datatype int identity)
ProductID (datatype int)
Image_Name (datatype text)

Requirement
To retrieve all images from the Images table associated with a specific ProductID from the Product table and in so doing present the image names contained in the Images table concatenated as a continous single field line display separating the respective values with a comma and then a space.

Implementation method
Call the function from a query by typically using this SQL syntax

SELECT ProductID, UDF_RetrieveImages(ProductID) as Images FROM Product


Functional Resolution
Create a user defined function having the following syntax

Expand|Select|Wrap|Line Numbers
  1.  
  2. CREATE FUNCTION dbo.UDF_RetrieveImages
  3. ( @ProductID int )
  4. RETURNS varchar(500)
  5. AS
  6. BEGIN
  7. DECLARE @ImageString varchar(255), @delimiter char
  8. SET @delimiter = ','
  9. SELECT @ImageString = COALESCE(@ImageString + @delimiter, '') +SPACE(1)+ Image_Name FROM .dbo.Images where ProductID=@ProductID order by ID
  10. RETURN ( SELECT LTRIM(@ImageString) AS [Images])
  11. END
  12.  

Comments & considerations
Pay attention to the defined varchar lengths within the body of the function to ensure it adequately reflects your needs

The resultant SQL statement when run as a view/query outlined above should present you with what you require from your posting

Regards

Jim :)
Oct 18 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

66
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
1
by: sarah | last post by:
Hi.. I'm having a problem trying to figure out the best way to process a POST and add items to my database, and hope someone can help me... :) Background: I have a database that I will use to...
53
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is...
6
by: George | last post by:
Hi All, I thought this would not compile because no return value is specified. But it does compile and run (aix and xlc v7.0.) Could someone kindly please point me to where in the spec this...
2
by: John Baker | last post by:
Hi: I have two systems, one a W98 and the other XP Home. I have Access 2000 installed on both, and have run into a difference in the way the two behave. I have a table on which I wish to reset...
7
by: MLH | last post by:
?dcount("","qryOwnrsDueITSwMissingAddr") when run in the immediate window return a number greater than the number of rows that display when the saved query is run - opening in the database window?...
3
by: abcd | last post by:
I have a linux machine (ip = 10.10.10.8), which can ping other machines on the same subnet...such as 10.10.10.1 10.10.10.2 10.10.10.5 10.10.10.6 10.10.10.254 If I use...
1
by: Peader | last post by:
Hi, I'm trying to complete a form that will search previous results entered in a table and return the row items based on a ID number. My current code is below: Private Sub cmd22_Click() ...
23
by: Himanshu Chauhan | last post by:
Hi! I was wondering, In the first parse of a singly linked list of unknown length, is it possible to know when we are at middle of the linked list? Regards --Himanshu
4
by: omono84 | last post by:
I know that this should be rather simple but i seem to be missing a step to get it to work. and have been unable to find a solution on the net. The aim is that I click on the open button to find...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.