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 =(给定pid的subcatid)。
有两个问题
只应显示1个输出而不是2
第二次输出无法正常工作
答案 0 :(得分: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
)
答案 1 :(得分:1)
未分配@subcate
参数:
select @subcate = subcate_id from product where pid=@pid
select * from product where product.subcate_id = @subcate
答案 2 :(得分:0)
您也可以在一个声明中做到这一点
SELECT
P1.*
FROM
product P1
JOIN
prodcut P2
ON p1.subcate_id = p2.subcate_id
WHERE
P2.pid = @pid
并且不用担心设置中间变量。