Solved

Stored Procedure to return related products

Posted on 2009-05-19
8
330 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
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…

757 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

20 Experts available now in Live!

Get 1:1 Help Now