返回过程执行代码在MySQL
问题描述:
我有一个存储过程返回过程执行代码在MySQL
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetNotExecutedOrders`(IN contractID INT(11))
BEGIN
SELECT idContract, idOrder, ProductID, Quantity, (SUM(`order`.Quantity))*(product.Price) as 'Total amount'
FROM typography.contract
JOIN typography.`order` ON contract.idContract=`order`.ContractID
JOIN typography.product ON `order`.ProductID=product.idProduct
WHERE idContract=contractID and ExecutionDate IS NULL
GROUP BY idOrder
ORDER BY idOrder;
END
我需要那么它是否有与该合同ID没有合同返回的执行代码<> 0来解决它,和合同列表如果与该合同ID有合同,则执行代码= 0。
答
使用代码的OUT
参数。然后使用COUNT(*)
查询进行设置。
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetNotExecutedOrders`(IN contractID INT(11), OUT executionCode INT)
BEGIN
SELECT COUNT(*) > 0 INTO executionCode
FROM typography.contract
JOIN typography.`order` ON contract.idContract=`order`.ContractID
JOIN typography.product ON `order`.ProductID=product.idProduct
WHERE idContract=contractID and ExecutionDate IS NULL;
IF (executionCode)
SELECT idContract, idOrder, ProductID, Quantity, (SUM(`order`.Quantity))*(product.Price) as 'Total amount'
FROM typography.contract
JOIN typography.`order` ON contract.idContract=`order`.ContractID
JOIN typography.product ON `order`.ProductID=product.idProduct
WHERE idContract=contractID and ExecutionDate IS NULL
GROUP BY idOrder
ORDER BY idOrder;
END IF;
END
+0
非常感谢!但是,你能解释一下COUNT(*)> 0是如何工作的吗?我不明白为什么它可以在没有IF语句的情况下使用 – 2015-02-06 01:20:45
+0
比较函数和运算符计算为'true'或'false',在MySQL中表示为'1'和'0'。 – Barmar 2015-02-06 01:21:24
使用'IF'语句。 – Barmar 2015-02-06 00:47:13
是的,我试过,但我不知道如何返回代码本身。你能帮我解决吗? – 2015-02-06 00:49:21
我建议为代码添加一个'OUT'参数。 – Barmar 2015-02-06 00:53:46