-5
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

上面的 sql 查询无法正常工作。

我只想要那些具有 subcatid=(subcatid of given pid) 的产品的产品详细信息。

有2个问题

  1. 应该只出现 1 个输出而不是 2

  2. 第二个输出不能正常工作

4

3 回答 3

2

尝试这个:

alter proc [dbo].[SpGetRelatedProducts](@pid int)
as

select *
from product as p1
where p1.subcate_id in (
                         select p2.subcate_id 
                         from product as p2
                         where p2.pid = @pid
                       )
于 2012-10-10T09:19:50.703 回答
1

@subcate分配参数:

select @subcate = subcate_id from product where pid=@pid

select * from product where product.subcate_id = @subcate
于 2012-10-10T09:18:50.007 回答
0

您也可以在一份声明中完成这一切

SELECT
               P1.* 
    FROM
             product P1
        JOIN
             prodcut P2
                 ON p1.subcate_id = p2.subcate_id
    WHERE
        P2.pid = @pid

而不必担心设置中间变量。

于 2012-10-10T09:27:38.750 回答