存储过程中的mysql奇怪的行为

时间:2012-02-10 16:00:14

标签: mysql sql database phpmyadmin

此存储过程负责材料预留。基本的想法是,它检查已经雇用了多少这种材料,如果库存中仍有材料,我将插入新订单。如果此预订ID已经预订了此材料,我将更新金额。

也许我做错了什么但是当我尝试添加一个新的预订时它有效。更新永远不会工作,当已经有特定材料ID的预订时,不可能将其与另一个预订ID一起租用。

我举个例子:

CALL aantal_besch_mat_van_tot('2007-03-13','2007-03-14',15,6,50,'procedure test lol');

好的,所以这样可行但是当我再次执行它时,50的数量应该是50 +之前的所以它应该是100,它永远不会更新。

此外,当您使用相同的材​​料和另一个预订ID时,它无效。 例如:

CALL aantal_besch_mat_van_tot('2007-03-13','2007-03-14',15,7,50,'Im hiring this material');

这也行不通。

我已经打印出了al值,并且获得了正确的金额。

您可以在此处找到我的存储过程代码:

    DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `fabiola`.`aantal_besch_mat_van_tot`(IN `p_datum_van` date,IN `p_datum_tot` date,IN `p_mat_id` int, IN `p_res_id` int, IN `p_nodig` int, IN `p_mat_opmerking` VARCHAR(255))

BEGIN
    DECLARE aantal INT DEFAULT 0;
    DECLARE tot  INT DEFAULT 0;
    DECLARE upda INT DEFAULT 0;
    DECLARE nieuwa INT DEFAULT 0;
    DECLARE voriga INT DEFAULT 0;
    DECLARE test INT DEFAULT 0;
    DECLARE res_van datetime;
    DECLARE res_tot datetime;

    -- Overeenkomstige data selecteren
    SELECT r.incheckdatum, r.uitcheckdatum FROM reservaties r
    WHERE r.id = p_res_id
    INTO res_van, res_tot;

        SELECT mpr.aantal FROM materialen_per_reservatie mpr
        WHERE mpr.materialen_id = p_mat_id AND
            (
            (p_datum_van >= mpr.datum_van AND p_datum_tot <= mpr.datum_tot) -- overlap: binnen
            OR (p_datum_van <= mpr.datum_van AND p_datum_tot >= mpr.datum_van) -- overlap: voor+in
            OR (p_datum_van <= mpr.datum_tot AND p_datum_tot >= mpr.datum_tot) -- overlap: na+in
            OR (p_datum_van <= mpr.datum_van AND p_datum_tot >= mpr.datum_tot) -- overlap:omsluitend
            )
        INTO aantal;

        -- The total amount of materials
        SELECT m.aantal_beschikbaar FROM materialen m
        WHERE m.id = p_mat_id
        INTO tot;
        -- The test variable is holding the amount of materials that's still available
        SELECT tot-aantal INTO test;
        -- Checking if im not ordering more then there is available
        IF p_nodig < test THEN
            -- Checking if this material is already hired from this reservation id
            SELECT mpra.id, mpra.aantal FROM materialen_per_reservatie mpra
            WHERE (mpra.reservaties_id = p_res_id
            AND   mpra.materialen_id = p_mat_id) INTO upda, voriga;
            -- if voriga is bigger then zero it means that there is already an reservatie for this material for this reservation so im not inserting but updating
            IF voriga > 0 THEN
                -- Selecting the previous amount and add it with the p_nodig amount
                SELECT voriga+p_nodig INTO nieuwa;
                UPDATE materialen_per_reservatie SET materialen_per_reservatie.aantal = nieuwa WHERE reservaties_id = p_res_id AND materialen_id = p_mat_id;
            ELSE
                -- There is no reservation for this material with this reservation id so i my insert a new row
                INSERT INTO materialen_per_reservatie(reservaties_id, materialen_id, aantal, effectief_gebruikt, opmerking, datum_van, datum_tot) VALUES (p_res_id, p_mat_id, p_nodig, p_nodig, p_mat_opmerking, p_datum_van, p_datum_tot);
            END IF;
        END IF;
END$$

我测试了两个INSERT / UPDATE查询是分开的,它们正在运行。

1 个答案:

答案 0 :(得分:0)

您的日期值附近的报价在哪里?

试试这个:

call aantal_besch_mat_van_tot('2007-03-13', '2007-03-14', 15, 6, 50, 'Im hiring this material');

注意日期类似于'2007-03-13',而不是2007-03-13,字面意思是数字1991(即2007 - 3 - 13 = 1991年)