【问题标题】:mysql stored procedure error (1172, 'Result consisted of more than one row')mysql 存储过程错误(1172,'结果包含多于一行')
【发布时间】:2013-11-07 07:18:02
【问题描述】:

尝试从 django 运行以下存储过程时,我收到 OperationError (1172, 'Result contains more than one row') 知道我可能做错了什么吗?

-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `UpdatePrices`(IN storeId int, IN bottleSize VARCHAR(50))
BEGIN
    DECLARE amount DECIMAL(10,2); DECLARE isCustom INT DEFAULT 0;
    DECLARE changeType VARCHAR(50) DEFAULT 'State'; DECLARE updateType INT DEFAULT 0;

        IF bottleSize = '1000 Ml' THEN
            SELECT S1000IncreaseChoices INTO changeType FROM store_store WHERE StoreID = storeId;

            IF changeType = 'State' THEN
                SELECT updateType = 0;
            END IF;

            IF changeType = 'Flat' THEN
                SELECT S1000IncreaseAmount INTO amount FROM store_store WHERE StoreID = storeId;
                SELECT updateType = 1;
            END IF;


            IF changeType = 'Percent' THEN
                SELECT 1 - S1000IncreaseAmount/100 INTO amount FROM store_store WHERE StoreID = storeId;
                SELECT updateType = 2;
            END IF;
        END IF;

        IF updateType = 0 THEN
            update store_storeliquor SL 
            inner join liquor_liquor LL
            on liquorID_id = id
            set StorePrice = ShelfPrice
            where BottleSize = bottleSize
            and storeID_id = storeId
            and custom = 0;
        END IF;

        IF updateType = 1 THEN
            update store_storeliquor SL 
            inner join liquor_liquor LL
            on liquorID_id = id
            set StorePrice = OffPremisePrice + amount
            where BottleSize = bottleSize
            and storeID_id = storeId
            and custom = 0;
        END IF;

        IF updateType = 1 THEN
            update store_storeliquor SL 
            inner join liquor_liquor LL
            on liquorID_id = id
            set StorePrice = OffPremisePrice / amount
            where BottleSize = bottleSize
            and storeID_id = storeId
            and custom = 0;
        END IF;



END

我不确定这是否重要,但我像这样启动存储过程:

def priceupdate(request, store_id):

    cursor = connection.cursor()
    cursor.callproc("UpdatePrices", (store_id, '1000 ML'))
    cursor.close()
    return HttpResponseRedirect(request.META.get('HTTP_REFERER'))

【问题讨论】:

    标签: mysql django stored-procedures


    【解决方案1】:

    您的 SELECT...INTO 查询提供的结果集包含多条记录。 WHERE 过滤器不正确 - 它们比较两个相同的值 StoreID = storeId。将 IN storeId int 参数重命名为另一个名称。例如 - IN storeId_param int

    查询会是这样 -

    SELECT S1000IncreaseChoices INTO changeType FROM store_store WHERE StoreID = storeId_param;
    

    【讨论】:

    • 是的,解决了它。现在我要弄清楚为什么代码没有做它应该做的事情。
    猜你喜欢
    • 1970-01-01
    • 2012-03-19
    • 1970-01-01
    • 1970-01-01
    • 2011-01-15
    • 1970-01-01
    • 1970-01-01
    • 2018-03-07
    • 2021-03-21
    相关资源
    最近更新 更多