2个SQL查询在一个存储过程中

时间:2015-01-04 23:21:25

标签: sql sql-server stored-procedures

我可以在一个存储过程中运行2个查询吗?

CREATE PROCEDURE AddProd
   @Store_Name varchar(50),
   @Price int,
   @Prod_Name varchar(50),
   @Qty int,
   @ProductDescription varchar(50),
   @RatingSum int,
   @RatingCount int,
   @ProductImage varchar(50),
   @Prod_Date date,
AS
BEGIN
    SELECT S.Store_ID
    FROM Store S
    WHERE StoreName=@StoreName

    INSERT INTO Product (Store_ID, Price, Prod_Name, Qty, ProductDescription, RatingSum, RatingCount, ProductImage, Prod_Date)
    VALUES (S.Store_ID, @Price, @Prod_Name, @Qty, @ProductDescrpition, @RatingSum, @RatingCount, @ProductImage, @Prod_Date)
END
GO

对于上面的代码,我想通过给用户提供的STORE_NAME作为参数来检索STORE_ID。

我想在STORE_ID声明中使用此INSERT

我可以这样做吗?!

AKA,是第一个查询返回的S.store_ID,与我在“值”中使用的那个相同?

3 个答案:

答案 0 :(得分:1)

从技术上讲,您可以在一个查询中执行此操作:

INSERT INTO Product
        (Store_ID,  Price, Prod_Name, Qty, ProductDescription, RatingSum, RatingCount, ProductImage, Prod_Date)
SELECT S.Store_ID, @Price,@Prod_Name,@Qty,@ProductDescription,@RatingSum,@RatingCount,@ProductImage,@Prod_Date 
FROM Store S 
WHERE StoreName=@StoreName

我没有方便检查的测试数据,但您可能必须为该查询中的select子句中的每个列指定适当的名称,而不仅仅是变量名称。这可能不起作用的另一个原因是,如果您还想从存储过程中返回选定的storeID,但即使在这种情况下,您也可以添加OUTPUT子句:

INSERT INTO Product 
          (Store_ID,  Price, Prod_Name, Qty, ProductDescription, RatingSum, RatingCount, ProductImage, Prod_Date)
OUTPUT S.Store_ID
  SELECT S.Store_ID, @Price,@Prod_Name,@Qty,@ProductDescription,@RatingSum,@RatingCount,@ProductImage,@Prod_Date
  FROM Store S 
  WHERE StoreName=@StoreName

但对于标题问题,答案是肯定的;您可以在单个存储过程中执行多个语句。

答案 1 :(得分:0)

除非你想从sp中返回storeID删除该查询并将其放入插入

INSERT INTO Product (Store_ID,Price,Prod_Name,Qty,ProductDescription,RatingSum,RatingCount,ProductImage,Prod_Date)
values (
    (SELECT S.Store_ID FROM Store S WHERE StoreName=@StoreName),
    @Price,@Prod_Name,@Qty,@ProductDescrpition,@RatingSum,@RatingCount,@ProductImage,@Prod_Date)

答案 2 :(得分:0)

如果StoreID对于每个商店名称都是唯一的,您可以将其存储在变量中并在插入中使用它

CREATE PROCEDURE AddProd

    @Store_Name varchar(50),
    @Price int,
    @Prod_Name varchar(50),
    @Qty int,
    @ProductDescription varchar(50),
    @RatingSum int,
    @RatingCount int,
    @ProductImage varchar(50),
    @Prod_Date date,

    AS
    BEGIN
        DECLARE @StoreID [DataType]
        SELECT @StoreID = S.Store_ID
        FROM Store S
        WHERE StoreName=@StoreName

        INSERT INTO Product (Store_ID,Price,Prod_Name,Qty,ProductDescription,RatingSum,RatingCount,ProductImage,Prod_Date)
        values (@StoreID,@Price,@Prod_Name,@Qty,@ProductDescrpition,@RatingSum,@RatingCount,@ProductImage,@Prod_Date)
    END
    GO

在任何情况下,您都可以使用以下

 CREATE PROCEDURE AddProd

   @Store_Name varchar(50),
   @Price int,
   @Prod_Name varchar(50),
   @Qty int,
   @ProductDescription varchar(50),
   @RatingSum int,
   @RatingCount int,
   @ProductImage varchar(50),
   @Prod_Date date,

   AS
   BEGIN



    INSERT INTO Product (Store_ID,
    Price,
    Prod_Name,
    Qty,
    ProductDescription,
    RatingSum,
    RatingCount,
    ProductImage,
    Prod_Date
    )
    SELECT       
    S.Store_ID    
    @StoreID,
    @Price,
    @Prod_Name,
    @Qty,
    @ProductDescrpition,
    @RatingSum,
    @RatingCount,
    @ProductImage,
    @Prod_Date
     FROM Store S
                WHERE StoreName=@StoreName
            END
            GO