4

我想使用 request_time 参数自动设置 session_id,所以我选择了一个包含 case 语句的 mysql 存储过程。这里去。

create procedure upd_userinput(in  request_time timestamp, out user_session_id int) 

begin 

update user_input;

case request_time

when time(request_time) < '9:15:00' && time(request_time) > '8:15:00' 
then set user_session_id = 1; 

when  time(request_time)< '10:15:00' && time(request_time) > '11:15:00' 
then set user_session_id =2; 

end case; 
end
//

但是,在 // 之后输入时出现 1064 错误。我检查了 mysql 文档,我认为大小写语法是正确的。

请帮忙。

4

2 回答 2

8
delimiter //

create procedure upd_userinput(in request_time TIMESTAMP,out time TIME, out user_session_id INT)     

begin      

set time = time(request_time);    

case  
when time < '09:15:00' && time > '08:15:00' then      
set user_session_id = 1; 

when time < '10:15:00' && time > '11:15:00' then
set user_session_id = 2;  

else set user_session_id =3;

end case;  
end //
于 2011-03-02T13:24:35.317 回答
1

case 必须至少匹配给定语句之一,否则您将收到此错误。为避免错误,请在其中包含 else 。

create procedure upd_userinput(in  request_time timestamp, out user_session_id int) 
begin 
update user_input;
case request_time
when time(request_time) < '9:15:00' && time(request_time) > '8:15:00' then
set user_session_id = 1; 
when  time(request_time)< '10:15:00' && time(request_time) > '11:15:00' then
set user_session_id =2; 
else set user_session_id =0; 
end case; 
end
于 2015-05-11T16:53:47.443 回答