Solved

Stored Procedure to return related products

Posted on 2009-05-19
8
368 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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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…

734 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