2012-09-19 4 views
0

이 절차의 목표는 요일을 사용하여 서비스의 조정 가격을 결정하는 것입니다. 예를 들어, 주말에는 가격이 10 % 인상됩니다. 월요일과 금요일에 가격은 동일합니다. 화요일부터 목요일까지는 5 %가 감소합니다. 최종 메시지는 새로운 조정 가격이어야합니다.mysql 프로 시저에서 변수를 선언 할 수 없습니다.

나는 이것을 제대로하고 있다고 생각했습니다. 그러나 선언 값에 중대한 오류가 나타납니다.

도움이 될 것입니다. 고맙습니다.

Create procedure DayOfWeek(inout p_price decimal(7,2), inout p_date date, out v_msg varchar(10)) 
begin 
    declare increase10 double; 
    set increase10 = p_price * (1.1); 

    declare decrease5 double; 
    set decrease5 = p_price * (0.95); 

    declare increase10cast varchar(10); 
    set increase10cast := cast(increase10 as char); 

    declare decrease5cast varchar(10); 
    set decrease5cast := cast(decrease10 as char); 

    declare regular varchar(10); 
    set regular := cast(p_price as char); 

    case 
    when p_price is null then 
     set v_msg := 'null'; 
    when p_date is null then 
     set v_msg := 'null'; 
    when weekday(p_date) in (0, 6) then 
     set v_msg := increase10cast; 
    when weekday(p_date) in (1, 5) then 
     set v_msg := regular; 
    when weekday(p_date) in (2, 2, 4) then 
     set v_msg := decrease5cast; 
    end case; 

end; 
# 
+0

오류 메시지를 표시 할 수 있습니까? – endyourif

답변

1

는 선언 모든 변수를 다음 SET 문에 갈 첫째 -

Create procedure a05_AdjustPrice_ByDayOfWeek(in p_price decimal(7,2), in p_date date, out v_msg varchar(10)) 
begin 
    declare increase10 double; 
    declare decrease5 double; 
    declare increase10cast varchar(10); 
    declare decrease5cast varchar(10); 
    declare regular varchar(10); 

    set increase10 = p_price * (1.1);  
    set decrease5 = p_price * (0.95);  
    set increase10cast := cast(increase10 as char);  
    set decrease5cast := cast(decrease5 as char);  
    set regular := cast(p_price as char); 

    case 
    when p_price is null then 
     set v_msg := 'null'; 
    when p_date is null then 
     set v_msg := 'null'; 
    when weekday(p_date) in (0, 6) then 
     set v_msg := increase10cast; 
    when weekday(p_date) in (1, 5) then 
     set v_msg := regular; 
    when weekday(p_date) in (2, 2, 4) then 
     set v_msg := decrease5cast; 
    end case; 

end; 

시작 ... 끝 블록의 시작에 있어야합니다 문을 선언합니다. 참조 Docs

IN 매개 변수를 IN 매개 변수로 변경하십시오. 다음과 같이 실행할 수 있습니다. -

call a05_AdjustPrice_ByDayOfWeek(100, '2012-09-09', @msg); 
select @msg; 
+0

감사합니다! 다시 도움이된다고 생각한다면 왜 v_msg에 대한 결과를 얻지 못합니까? 내가 뭔가를하려고한다면 a05_AdjustPrice_ByDayOfWeek (100, '2012-09-09', @msg) # – user1682055

+0

내 편집을보세요. – Typist

+0

정말 고마워요. – user1682055