【问题标题】:Returning procedure execution code in MySQL在 MySQL 中返回过程执行代码
【发布时间】:2015-02-06 00:34:34
【问题描述】:

我有一个存储过程

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。

【问题讨论】:

  • 使用IF 声明。
  • 是的,我试过了,但我不知道如何返回代码本身。你能帮我解决这个问题吗?
  • 我建议为代码添加一个OUT参数。

标签: mysql stored-procedures mysql-workbench


【解决方案1】:

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

【讨论】:

  • 非常感谢!但是你能解释一下 COUNT(*) > 0 是如何工作的吗?我不明白为什么它可以在没有 IF 语句的情况下使用
  • 比较函数和运算符求值为truefalse,在MySQL中分别表示为10
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-26
  • 1970-01-01
  • 2015-10-14
  • 1970-01-01
  • 1970-01-01
  • 2015-08-13
相关资源
最近更新 更多