在MySQL中返回过程执行代码

时间:2015-02-06 00:34:35

标签: mysql stored-procedures mysql-workbench

我有一个存储过程

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。

1 个答案:

答案 0 :(得分: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