该存储过程正在处理物料预留。基本的想法是,它检查这些材料已经被雇佣了多少,如果还有材料库存,我会插入一个新的订单。如果此预订ID已为此材料预留,我正在更新金额。mysql中奇怪行为的存储过程
也许我做错了什么,但当我尝试添加一个新的预订它的作品。更新从不起作用,并且当已经有特定物料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查询的独立和他们的工作。
什么是隔离级别?你有没有试图把它交易? – 2012-02-10 16:07:59
对不起,这是一个错字。我已经在使用''日期 – 2012-02-10 16:19:59