Click here to Skip to main content
15,916,189 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
USE [cms]
    GO
    /****** Object:  StoredProcedure [dbo].[SpGetRelatedProducts]    Script Date: 10/10/2012 14:35:39 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    ALTER proc [dbo].[SpGetRelatedProducts](@pid int)
    AS
    DECLARE @subcate int;
    select subcate_id as subcate from product where pid=@pid
    SELECT *
    FROM product
    where product.subcate_id=@subcate
       
    exec SpGetRelatedProducts 2

the given above sql query not working correctly as wanted.......???
i want the aproduct details of only those , which has subcatid=(subcatid of given pid)..???
there r 2 issues
1.... only 1 output should appear instead of 2

2.... 2nd one not working correctly(second output)
Posted
Updated 9-Oct-12 23:20pm
v2
Comments
AshishChaudha 10-Oct-12 5:29am    
Please provide the table structure of both the tables....

Try following
SQL
select * from product where subcate_id in (SELECT subcate_id FROM product where pid=@pid)


The above query gives the details of all the products which has subcate_id of the subcate_Id matches in product.

Thanks
 
Share this answer
 
Comments
[no name] 10-Oct-12 8:35am    
my 5
damodara naidu betha 11-Oct-12 1:05am    
Hi,
I have a doubt. Why we need to check twice in same product table?. We can do the same like select * from product where pid=@pid.. instead of using the sub query.Both select * from product where subcate_id in (SELECT subcate_id FROM product where pid=@pid) and select * from product where pid=@pid give same result and select * from product where pid=@pid is more efficient than your query because it is avoiding 'in' and sub query.
AshishChaudha 11-Oct-12 1:28am    
I asked for the table structure..as per your requirement, I told you this query..
Hi ... use this code block...


SQL
ALTER proc [dbo].[SpGetRelatedProducts](@pid int)
AS
SELECT * FROM product
where product.pid=@pid


Thank you
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900