Solved

Stored Procedure to return related products

Posted on 2009-05-19
8
341 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
  • 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
 
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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…

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now