MySql存储过程 - 错误1172(42000):结果由多行组成

时间:2018-05-09 18:14:28

标签: mysql stored-procedures database-cursor

我根据数据库中的一些折扣方案编写了一个存储过程来计算净产品数量....存储过程编译成功但在运行时我得到 ERROR 1172(42000):结果由多行组成

我的代码如下,

    delimiter //
    CREATE PROCEDURE calculateNetItemAmount(IN productId INT,IN quantity INT, OUT netItemAmount DOUBLE)
    BEGIN
        DECLARE discountPer DOUBLE DEFAULT 0;

        SELECT `SellingUnitPrice` into netItemAmount 
        FROM `TBL_PRODUCT_MASTER` WHERE `Id` = productId;
        SET netItemAmount = quantity * netItemAmount ;

        SELECT discDetail.`DiscountPercentage` into discountPer 
        FROM `TBL_DISCOUNT_DETAIL` AS discDetail , `TBL_DISCOUNT` AS disc, TBL_PRODUCT_DISCOUNT AS prodDisc 
        WHERE disc.`DiscountStartDate` < NOW() 
        AND disc.`DiscountEndDate` > NOW() AND disc.`IsEnabled` = 1 
        AND disc.`SchemeType` = 'Quantity Discount' 
        AND discDetail.DiscountId = disc.Id AND prodDisc.productId = productId 
        AND prodDisc.`IsEnabled` = 1 ;

        IF discountPer IS NOT NULL THEN 
           SET netItemAmount = netItemAmount * (1 - discountPer * 0.01)        
       END IF;

        SELECT discDetail.`DiscountPercentage` into discountPer 
        FROM `TBL_DISCOUNT_DETAIL` AS discDetail , `TBL_DISCOUNT` AS disc, TBL_PRODUCT_DISCOUNT AS prodDisc 
        WHERE disc.`DiscountStartDate` < NOW() AND disc.`DiscountEndDate` > NOW() 
        AND disc.`IsEnabled` = 1 AND disc.`SchemeType` = 'Volume Discount' 
        AND discDetail.DiscountId = disc.Id AND prodDisc.productId = productId 
        AND prodDisc.`IsEnabled` = 1 ;

        IF discountPer IS NOT NULL THEN 
           SET netItemAmount = netItemAmount * (1 - discountPer * 0.01)
        END IF;

    END//
    delimiter ;

输出:

mysql> CALL calculateNetItemAmount(1, 2, @RES);
ERROR 1172 (42000): Result consisted of more than one row

请帮帮我...提前谢谢你..

1 个答案:

答案 0 :(得分:0)

您必须确保只需要一行的查询,只返回一行。

例如,您的查询使用into句子:

    SELECT `SellingUnitPrice` into netItemAmount 
    FROM `TBL_PRODUCT_MASTER` WHERE `Id` = productId;

有很多原因导致查询返回多行(数据不一致,重复记录......任何原因)

一个建议(但不推荐)解决方案是在句子末尾添加limit 1

    SELECT `SellingUnitPrice` into netItemAmount 
    FROM `TBL_PRODUCT_MASTER` WHERE `Id` = productId
    LIMIT 1;