Solved

Stored Procedure to return related products

Posted on 2009-05-19
8
373 Views
Last Modified: 2012-05-07
Hi Guys,

I have a table that holds products and has a primary key field called ID. I have a second table called tblRelatedProducts which looks as below

ID  ItemID RelatedProductID
1      3               10
2      3               11
3      3               12

I need to be able to write a stored procedure that I can pass ItemId = 3 and returns all the product informaiton about products 10, 11, 12 as these are realted to Item 3.

Please can you point me in the right direction on how I can achieve this in a stored procedure?

Thanks in advance.

Regards,
Nigel
0
Comment
Question by:Nigel_Taylor
[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
  • 3
  • 3
  • 2
8 Comments
 
LVL 5

Expert Comment

by:satheeshm
ID: 24421467
CREATE PROCEDURE [OWNER].[PROCEDURE NAME] AS
@itemid as int

AS

Begin

ID  ItemID RelatedProductID

SELECT tblRelatedProducts.*

From tblRelatedProducts Inner Join Products ON tblRelatedProducts.ItemID = Products.ItemID

WHERE Products.ItemID = @itemid


End
0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24421480
You can do it in a single query like this:

select * from tblRelatedProducts
where ItemID  = ?

where ? represents your input value

or Else if you need it in a procedure then this will do

CREATE PROCEDURE test( @itemid int)
AS
      SELECT * FROM tblRelatedProducts
        where ItemID  = @itemid
GO

Hope this helps
0
 

Author Comment

by:Nigel_Taylor
ID: 24421605
Sorry guys I might have miss worded the question.

I am fine with getting the ID's of the related products based on the input value so getting 10,11,12 when 3 is the input value but I need to convert 10 into Paperclips, 11 into paper, 12 into pens as the 10,11,12 are item ID's.

Sorry for the confusion and hope thats makes a bit more sense.

Regrds,
Nigel
0
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
LVL 5

Expert Comment

by:satheeshm
ID: 24421641
are those descriptions stored in another table. Can you pls provde both table structures. so that it will be easy to give you correct answer.
0
 
LVL 57

Assisted Solution

by:Raja Jegan R
Raja Jegan R earned 55 total points
ID: 24421651
Kindly change the column and table names accordingly since you haven't mentioned about the structure of your products table:
CREATE PROCEDURE test( @itemid int)
AS
      SELECT t1.ID, t1.ItemID, t1.RelatedProductID, t2.Productname
      FROM tblRelatedProducts t1, products t2
      WHERE t1.RelatedProductID = t2.ProductId
      AND t1.ItemID  = @itemid
GO

Open in new window

0
 

Author Comment

by:Nigel_Taylor
ID: 24421748
Yeah no probs,

tblRelatedProducts
id    fldProductId        fldRelatedProductId
1              3                                 1
1              3                                 2

tblItems
id      fldTitle      fldImage      fldPrice
1       Pencils    pencils.gif    2.99
2       Paper       paper.gif      1.99
3       Pens        pens.gif        3.99
So in this example if I was to enter 3 into the stored procedure I would expect to see the following restult set as these products are related to item 3
id      fldTitle      fldImage      fldPrice
1       Pencils    pencils.gif    2.99
2       Paper       paper.gif      1.99  
Hope this helps,
Reagrds,
Nigel
0
 
LVL 5

Assisted Solution

by:satheeshm
satheeshm earned 45 total points
ID: 24421793
okay..
here is the procedure

CREATE PROCEDURE test( @itemid int)
AS
      SELECT t2.ID, t2.fldTitle, t2.fldImage, t2.fldPrice
      FROM tblRelatedProducts t1, tblItems t2
      WHERE t1.RelatedProductID = t2.Id
      AND t1.fldProductId = @itemid
GO
0
 

Accepted Solution

by:
Nigel_Taylor earned 0 total points
ID: 24421863
Thanks guys thats perfect and thanks for the quick responses.

Regards,

Nigel
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

630 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