Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Stored Procedure to return related products

Posted on 2009-05-19
8
Medium Priority
?
380 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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 220 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 180 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

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.

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

670 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