错误1172(42000):结果包含多行

gywdnpxw  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(540)

我已经写了一个存储过程来计算净产品金额的基础上,一些折扣方案在数据库中可用。。。。存储过程编译成功,但在运行时出现错误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

请帮帮我…先谢谢你。。

wz1wpwve

wz1wpwve1#

必须确保只需要一行的查询只返回一行。
例如,您的查询使用 into 句子:

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

查询返回多行的原因有很多(数据不一致,重复记录。。。(任何原因)
一个建议的(但不建议的)解决方案是add limit 1 直到句末。

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

相关问题