我正在制作一个存储过程,根据一个人每个月的总交易数进行返现,但是当被调用时,它会显示返回的空值
CREATE DEFINER=`root`@`localhost` PROCEDURE `stored_procedure_cashback`(IN a INT, IN b INT)
BEGIN
DECLARE cashback_get INT;
DECLARE total INT;
SELECT customers.`id_customers`, customers.`customers_name`, MONTH(transaction.`transaction_date`) AS month,
YEAR(transaction.`transaction_date`) AS year,
SUM((transaction_detail.`ammount`*transaction_detail.`price_per_piece`)-transaction_detail.`discount`) AS total,
cashback_get
FROM transaction_detail
INNER JOIN transaction ON transaction_detail.`id_transaction`=transaction.`id_transaction`
INNER JOIN customers ON transaction.`id_customers`=customers.`id_customers`
WHERE MONTH(transaction.`transaction_date`) = a AND YEAR(transaction.`transaction_date`) = b
GROUP BY customers.`id_customers`;
IF (total >= 20000) THEN
SET cashback_get = 2000;
ELSE
SET cashback_get = 0;
END IF;

答案 0 :(得分:0)
你需要将结果作为OUT参数。
CREATE DEFINER=`root`@`localhost` PROCEDURE.
`stored_procedure_cashback`(IN a INT, IN b INT, OUT cashback_get INT)
BEGIN
DECLARE total INT;
SELECT Sum( etc etc) into total
From table
etc etc
IF total >= 20000 Then
SET cashback_get = 2000;
ELSE
SET cashback_get = 0;
END IF;
END;