存储过程中关键字end附近的语法不正确?

时间:2014-11-03 06:11:10

标签: sql-server

CREATE PROCEDURE M_SelectStockIN @FarmID   int, 
                                 @ItemName varchar(50), 
                                 @InType   varchar(5) 
AS 
  BEGIN 
      select ( Sum(( m.Item_Qty )) - total ) As InStock, 
             * 
      from   (select Sum(d.Use_Qty) as total 
              from   MoryaSales m 
                     inner join DailyFarmEntry d 
                             on m.batchID = d.FarmID 
              where  m.batchID = @FarmID 
                     and d.FarmID = @FarmID 
                     and m.Tr_Type = @InType 
                     and d.In_Type = @InType 
                     and m.Item_Name = @ItemName 
                     and d.ItemName = @ItemName) 
  END 

RETURN 

我需要帮助来解决该错误:   当我们运行上面的存储过程时,错误"     语法错误"发生在关键字end附近

2 个答案:

答案 0 :(得分:1)

这可能有所帮助:

删除RETURN并在内部查询中添加别名:

CREATE PROCEDURE M_SelectStockIN
    @FarmID int,
    @ItemName varchar(50),
    @InType varchar(5)
AS
BEGIN        
select 
    (sum((m.Item_Qty)) - total) As InStock,
    * 
from( -- marks the start of the inner query
    select 
        sum(d.Use_Qty) as total 
    from MoryaSales m 
    inner join DailyFarmEntry d 
        on m.batchID = d.FarmID 
    where 
        m.batchID = @FarmID 
        and d.FarmID = @FarmID 
        and m.Tr_Type = @InType 
        and d.In_Type = @InType 
        and m.Item_Name = @ItemName 
        and d.ItemName = @ItemName
    )t -- You need to put an alias for this inner query to prevent the error from happening.
END

答案 1 :(得分:0)

您缺少表格和删除RETURN的别名

 select (sum((m.Item_Qty)) - total) As InStock,* from
    (select sum(d.Use_Qty) as total from MoryaSales m 
    inner join DailyFarmEntry d on m.batchID = d.FarmID 
    where m.batchID = @FarmID and d.FarmID = @FarmID 
    and m.Tr_Type = @InType and d.In_Type = @InType 
    and m.Item_Name = @ItemName and d.ItemName = @ItemName)d 
                                                           ^