返回 MySQL 中的过程执行代码

Returning procedure execution code in 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

我需要修复它 returns 一个执行代码 <> 0 如果没有这样的合约 ID 的合约,如果有一个合约的合约列表和执行代码 = 0该合同 ID。

为代码使用 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