2011-03-23 30 views
1

我有两个输入参数shopidcategoryid的SQL Server:存储过程相关

我想创建一个存储过程categoryproductlist用于显示与categoryid产品。我有一个表productproductid , shopid , title列和我有一个表category的列shopid,title,logical code, categoryid

谁能告诉我如何写categoryproductlist这些输入参数的存储过程?

+0

我是新来的,你使用哪个数据库存储过程 – user 2011-03-23 17:06:15

+0

- 从标题SQL服务器?如果你可以编写select语句,那么你可以很容易地将它转换成一个存储过程或函数 - 你可以为此写一个select吗? – Rup 2011-03-23 17:07:38

+0

除非您通过ShopID关联它们,否则您需要向Products表中添加CategoryID列。否则,将假定所有产品都属于所有类别。 – squillman 2011-03-23 17:08:28

回答

2

有此一展身手:

CREATE PROCEDURE p_CategoryProductList 
    @CategoryID INT 
    @ShopID  INT 

AS 

SELECT 
    c.itle as CategoryTitle, 
    c.[logical code], 
    p.title as ProductTitle 
FROM Product p 
INNER JOIN tblcategoryproduct cp on p.ProductID=cp.ProductID 
INNER JOIN Category c on c.CategoryID=cp.CategoryID 
WHERE [email protected] 
AND [email protected] 
+1

我永远不会在存储过程中使用'SELECT *' - 拼写出你的列回报 - 这种方式不那么令人讨厌的惊喜! – 2011-03-23 17:52:10

+1

@marc_s对,谢谢。我只是把它扔在那里,忘了更新。我会解决的。 – squillman 2011-03-23 18:02:14