SQL存储过程-哪里出错了



我正在尝试创建一个存储过程,该过程通过客户是否已占用和标准费率来计算客户的总收入。我收到一条错误消息,当我尝试从中调用时,我得到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;

首先需要为输入参数提供不同于列的名称。然后你需要使用它们。此外,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 //

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;

将结果作为结果集返回比使用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);

最新更新