【问题标题】:SQL stored procedure- where am I going wrong?SQL 存储过程 - 我哪里出错了?
【发布时间】:2020-12-06 22:08:56
【问题描述】:

我正在尝试创建一个存储过程,该过程根据客户是否被占用和标准费率计算来自客户的总收入。我收到一条错误消息,当我尝试从中调用时,我得到 NULL。任何人都可以帮忙吗?谢谢。

//Delimiter
CREATE PROCEDURE calculateRevenue (in customerIDs int, OUT totalRevenue dec(15,2))
    BEGIN 
    SELECT SUM(Occupied*StandardRate) into totalRevenue FROM climatesouth
        WHERE customerIDs = customerID;
    END //
    delimiter//
    
call calculateTotal(10, @totalRevenue);
SELECT @totalRevenue;

【问题讨论】:

    标签: mysql sql stored-procedures


    【解决方案1】:

    首先,您需要为输入参数指定与列不同的名称。然后你需要使用它们。此外,DELIMITER 位于存储过程定义之前:

    DELIMITER //
    
    CREATE PROCEDURE calculateRevenue (
        in in_customerIDs int, 
        out out_totalRevenue dec(15,2))
    BEGIN 
        SELECT SUM(cs.Occupied cs.* cs.StandardRate) into out_totalRevenue
        FROM climatesouth cs
        WHERE cs.customerID = in_customerID;
    END //
    

    【讨论】:

      【解决方案2】:

      delimiter 分配已关闭:您将其设置为 //create procedure 语句之后。

      另外,参数名称需要修正:您在查询中没有使用正确的名称(您的参数有一个尾随's'),因此该过程不会产生您期望的结果。

      所以:

      delimiter //  -- change the default delimiter here
      
      create procedure calculaterevenue (in p_customerid int, out p_totalrevenue dec(15,2))
      begin 
          select sum(occupied * standardrate) into p_totalrevenue
          from climatesouth
          where customerid = p_customerid; -- "p_customerid" is the parameter name
      end //
      
      delimiter ; -- reset the delimiter, now we can call the procedure
          
      call calculaterevenue(10, @totalrevenue);
      select @totalrevenue;
      

      【讨论】:

        【解决方案3】:

        将结果作为结果集返回比使用 OUT 参数更容易。 OUT 参数通常仅在从另一个过程调用过程时使用。如果您从应用程序调用该过程,请使用结果集。

        delimiter //
        CREATE PROCEDURE calculateRevenue (in_customerID int)
        BEGIN 
          SELECT SUM(Occupied*StandardRate) as totalrevenue
          FROM climatesouth
          WHERE customerID = in_customerID;
        END
        //
        delimiter ;
        call calculateRevenue(10);
        

        【讨论】:

          猜你喜欢
          • 2010-11-06
          • 2014-03-01
          • 2016-08-02
          • 1970-01-01
          • 1970-01-01
          • 2011-07-21
          • 2011-03-14
          • 2013-12-25
          • 1970-01-01
          相关资源
          最近更新 更多