2012-02-10 7 views
1

이 저장 프로시 저는 재료 예약을 담당합니다. 기본 아이디어는이 소재가 이미 얼마나 많이 사용되었는지를 확인하고 재고가 남아있는 경우 새 주문을 삽입한다는 것입니다. 이 예약 ID에 대해이 자재에 대한 예약이 이미있는 경우 금액을 갱신 중입니다.mysql 이상한 동작의 저장 프로 시저

아마도 뭔가 잘못된 일을하고 있지만 새로운 예약을 추가하려고 할 때 작동합니다. 업데이트가 절대로 작동하지 않으며 특정 자재 ID에 대한 예약이 이미있는 경우 다른 예약 ID로 임대 할 수 없습니다. 이 작품 때문에

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

좋아하지만 난 다시 실행할 때 100 그것은 결코 업데이트해야하므로 양 (50)은 50 + 이전으로 이동해야합니다

나는 당신에게 예를 제공합니다.

또 다른 예약 ID로 동일한 자료를 대여 할 때도 작동하지 않습니다. 예 :

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

또한 작동하지 않습니다.

나는 벌써 알 가치를 인쇄하고 거기에 맞는 금액을 받고.

여기 내 저장 프로 시저 코드를 찾을 수 있습니다

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 쿼리의 별도 그들이 노력하고 있습니다.

+0

격리 수준은 무엇인가? 그것을 거래에 넣으려고 했습니까? –

+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 - = 1991 13)

+0

당신이 그것을 못살게 굴 것 같아요. –

+0

오타라고해서 유감입니다. 나는 이미 위에서 언급 한 것을 사용하고있다. –